Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 10x 11x 2x 2x 8x 8x 11x 1x 1x 7x 7x 7x 11x 19x 12x 12x 19x 7x 11x 7x 7x 7x 7x 7x 7x 11x 11x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 6x 6x 6x 7x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 7x 15x 15x 8x 15x 4x 4x 4x 4x 4x 4x 4x 4x 4x 15x 5x 5x 7x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 7x 4x 4x 4x 4x 4x 7x 7x 7x 7x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 7x 8x 1x 1x 1x 1x 1x 1x 6x 6x 8x 6x 6x 6x 8x 18x 14x 14x 18x 6x 6x 6x 8x 8x 8x 8x 8x 8x 6x 8x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 6x 6x 8x 5x 5x 5x 5x 5x 8x 8x 8x 6x 6x 6x 6x 6x 8x 8x 8x 1x 1x 1x 8x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 3x 4x 4x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 3x 3x 3x | /**
* Loyalty Tier Management Utility Functions
* Handles tier calculation, upgrades, and tier-related operations
*/
import { PrismaClient, LoyaltyTier } from "@prisma/client";
import { logger } from "@/lib/logging";
const LOG_CATEGORY = "LOYALTY_TIERS";
export interface TierInfo {
id: number;
name: string;
minPoints: number;
pointsMultiplier: number;
perks: Record<string, unknown>;
}
export interface TierStatus {
currentTier: TierInfo | null;
nextTier: TierInfo | null;
pointsToNextTier: number | null;
progress: number; // 0-100 percentage to next tier
}
export interface TierUpgradeResult {
upgraded: boolean;
previousTier: TierInfo | null;
newTier: TierInfo | null;
userId: number;
}
/**
* Get all active loyalty tiers
*/
export async function getActiveTiers(
prisma: PrismaClient
): Promise<LoyaltyTier[]> {
const program = await prisma.loyaltyProgram.findFirst({
where: { isActive: true },
include: {
tiers: {
orderBy: { minPoints: "asc" }}}});
return program?.tiers || [];
}
/**
* Calculate current tier for a user
*/
export async function calculateCurrentTier(
prisma: PrismaClient,
userId: number
): Promise<TierInfo | null> {
const loyalty = await prisma.customerLoyalty.findUnique({
where: { userId }});
if (!loyalty) {
return null;
}
const tiers = await getActiveTiers(prisma);
if (tiers.length === 0) {
return null;
}
// Find the highest tier the user qualifies for
let currentTier: LoyaltyTier | null = null;
for (const tier of tiers) {
if (loyalty.lifetimePoints >= tier.minPoints) {
currentTier = tier;
}
}
if (!currentTier) {
return null;
}
return {
id: currentTier.id,
name: currentTier.name,
minPoints: currentTier.minPoints,
pointsMultiplier: currentTier.pointsMultiplier,
perks: (currentTier.perks as Record<string, unknown>) || {}};
}
/**
* Get complete tier status for a user
*/
export async function getTierStatus(
prisma: PrismaClient,
userId: number
): Promise<TierStatus> {
const loyalty = await prisma.customerLoyalty.findUnique({
where: { userId }});
const tiers = await getActiveTiers(prisma);
if (!loyalty || tiers.length === 0) {
return {
currentTier: null,
nextTier: null,
pointsToNextTier: null,
progress: 0};
}
// Find current tier and next tier
let currentTier: LoyaltyTier | null = null;
let nextTier: LoyaltyTier | null = null;
let pointsToNextTier: number | null = null;
let progress = 0;
for (let i = 0; i < tiers.length; i++) {
const tier = tiers[i];
if (loyalty.lifetimePoints >= tier.minPoints) {
currentTier = tier;
} else if (!nextTier) {
nextTier = tier;
pointsToNextTier = tier.minPoints - loyalty.lifetimePoints;
// Calculate progress
const previousMin = currentTier?.minPoints || 0;
const range = tier.minPoints - previousMin;
const earned = loyalty.lifetimePoints - previousMin;
progress = range > 0 ? Math.round((earned / range) * 100) : 0;
}
}
// If user is at highest tier, progress is 100%
if (currentTier && !nextTier) {
progress = 100;
}
return {
currentTier: currentTier
? {
id: currentTier.id,
name: currentTier.name,
minPoints: currentTier.minPoints,
pointsMultiplier: currentTier.pointsMultiplier,
perks: (currentTier.perks as Record<string, unknown>) || {}}
: null,
nextTier: nextTier
? {
id: nextTier.id,
name: nextTier.name,
minPoints: nextTier.minPoints,
pointsMultiplier: nextTier.pointsMultiplier,
perks: (nextTier.perks as Record<string, unknown>) || {}}
: null,
pointsToNextTier,
progress};
}
/**
* Check if user should be upgraded to a new tier
*/
export async function checkTierUpgrade(
prisma: PrismaClient,
userId: number
): Promise<TierUpgradeResult> {
try {
const loyalty = await prisma.customerLoyalty.findUnique({
where: { userId }});
if (!loyalty) {
return {
upgraded: false,
previousTier: null,
newTier: null,
userId};
}
const tiers = await getActiveTiers(prisma);
if (tiers.length === 0) {
return {
upgraded: false,
previousTier: null,
newTier: null,
userId};
}
// Find the tier they should be at based on lifetime points
let targetTier: LoyaltyTier | null = null;
for (const tier of tiers) {
if (loyalty.lifetimePoints >= tier.minPoints) {
targetTier = tier;
}
}
// Get current tier from database
const previousTier = loyalty.currentTierId
? tiers.find((t) => t.id === loyalty.currentTierId) || null
: null;
// Check if upgrade is needed
const upgraded = Boolean(
targetTier &&
(!previousTier || targetTier.minPoints > previousTier.minPoints)
);
if (upgraded && targetTier) {
// Update tier in database
await prisma.customerLoyalty.update({
where: { id: loyalty.id },
data: { currentTierId: targetTier.id }});
logger.info("Tier upgraded", {
category: LOG_CATEGORY,
userId,
previousTier: previousTier?.name || "None",
newTier: targetTier.name});
}
return {
upgraded,
previousTier: previousTier
? {
id: previousTier.id,
name: previousTier.name,
minPoints: previousTier.minPoints,
pointsMultiplier: previousTier.pointsMultiplier,
perks: (previousTier.perks as Record<string, unknown>) || {}}
: null,
newTier: targetTier
? {
id: targetTier.id,
name: targetTier.name,
minPoints: targetTier.minPoints,
pointsMultiplier: targetTier.pointsMultiplier,
perks: (targetTier.perks as Record<string, unknown>) || {}}
: null,
userId};
} catch (error) {
logger.error("Error checking tier upgrade", error instanceof Error ? error : new Error(String(error)), { category: LOG_CATEGORY });
throw error;
}
}
/**
* Get tier benefits/perks
*/
export async function getTierPerks(
prisma: PrismaClient,
tierId: number
): Promise<Record<string, unknown>> {
const tier = await prisma.loyaltyTier.findUnique({
where: { id: tierId }});
return (tier?.perks as Record<string, unknown>) || {};
}
/**
* Check if user has a specific tier perk
*/
export async function hasTierPerk(
prisma: PrismaClient,
userId: number,
perkKey: string
): Promise<boolean> {
const currentTier = await calculateCurrentTier(prisma, userId);
if (!currentTier || !currentTier.perks) {
return false;
}
return perkKey in currentTier.perks && currentTier.perks[perkKey] === true;
}
/**
* Get all users in a specific tier
*/
export async function getUsersInTier(
prisma: PrismaClient,
tierId: number
): Promise<number[]> {
const users = await prisma.customerLoyalty.findMany({
where: { currentTierId: tierId },
select: { userId: true }});
return users.map((u) => u.userId);
}
/**
* Get tier distribution statistics
*/
export async function getTierDistribution(
prisma: PrismaClient
): Promise<{ tierId: number; tierName: string; userCount: number }[]> {
const tiers = await getActiveTiers(prisma);
const distribution = [];
for (const tier of tiers) {
const count = await prisma.customerLoyalty.count({
where: { currentTierId: tier.id }});
distribution.push({
tierId: tier.id,
tierName: tier.name,
userCount: count});
}
// Add users with no tier
const noTierCount = await prisma.customerLoyalty.count({
where: { currentTierId: null }});
if (noTierCount > 0) {
distribution.unshift({
tierId: 0,
tierName: "No Tier",
userCount: noTierCount});
}
return distribution;
}
|