From 8b77f23eb4754966951c0833753d8015bb2f5b5d Mon Sep 17 00:00:00 2001 From: JayWebtech Date: Sat, 26 Apr 2025 18:57:06 +0100 Subject: [PATCH] feat : impact product level --- src/controllers/rewards.controller.ts | 148 ++++++++++++++------------ src/routes/rewards.routes.ts | 4 +- src/routes/social.routes.ts | 6 +- 3 files changed, 85 insertions(+), 73 deletions(-) diff --git a/src/controllers/rewards.controller.ts b/src/controllers/rewards.controller.ts index 0e157d9..2cc8e45 100644 --- a/src/controllers/rewards.controller.ts +++ b/src/controllers/rewards.controller.ts @@ -1,77 +1,89 @@ -import type { Request, Response } from 'express'; -import { eq } from 'drizzle-orm'; -import { db } from '../db'; -import { users } from '../db/schema'; +import type { Request, Response } from "express"; +import { eq } from "drizzle-orm"; +import { db } from "../db"; +import { users } from "../db/schema"; -// Map numeric levels to impact level enum -function getImpactLevel(level: number): 'NEWBIE' | 'PRO' | 'HERO' | 'GUARDIAN' { - if (level <= 3) return 'NEWBIE'; - if (level <= 6) return 'PRO'; - if (level <= 9) return 'HERO'; - return 'GUARDIAN'; +function getImpactLevel(level: number): "NEWBIE" | "PRO" | "HERO" | "GUARDIAN" { + if (level <= 3) return "NEWBIE"; + if (level <= 6) return "PRO"; + if (level <= 9) return "HERO"; + return "GUARDIAN"; } -export const claimReward = async (req: Request, res: Response): Promise => { - try { - const userId = req.user?.id; - const { impactLevel } = req.body; - - if (!userId || !impactLevel) { - res.status(400).json({ - success: false, - message: 'Missing required parameters', - }); - return; - } +function getCongratulatoryMessage(level: number): string { + if (level === 1) { + return "Congratulations! You've just received the first level of DeCleanup Impact Product. Come back for more! Share your referral with friends and earn more DCU Points."; + } else if (level === 10) { + return "Congratulations! You've successfully completed all levels of DeCleanup journey at this phase! Stay updated for new level updates."; + } else { + return "Congratulations! You've just upgraded to a higher level of DeCleanup Impact Product. Come back for more!"; + } +} - // Convert numeric level to enum - const level = parseInt(impactLevel); - if (isNaN(level) || level < 1 || level > 10) { - res.status(400).json({ - success: false, - message: 'Invalid impact level. Must be between 1 and 10.', - }); - return; - } +export const claimReward = async ( + req: Request, + res: Response +): Promise => { + try { + const userId = req.user?.id; + const { impactLevel } = req.body; - // Get user data - const user = await db.query.users.findFirst({ - where: eq(users.id, userId), - }); + if (!userId || !impactLevel) { + res.status(400).json({ + success: false, + message: "Missing required parameters", + }); + return; + } - if (!user) { - res.status(404).json({ - success: false, - message: 'User not found', - }); - return; - } + const level = parseInt(impactLevel); + if (isNaN(level) || level < 1 || level > 10) { + res.status(400).json({ + success: false, + message: "Invalid impact level. Must be between 1 and 10.", + }); + return; + } - // Check if user has connected Twitter - const hasTwitter = !!user.twitterHandle; - - // Update user's impact level - await db.update(users) - .set({ - impactLevel: getImpactLevel(level), - updatedAt: new Date(), - }) - .where(eq(users.id, userId)); + const user = await db.query.users.findFirst({ + where: eq(users.id, userId), + }); - res.status(200).json({ - success: true, - data: { - numericLevel: level, - impactLevel: getImpactLevel(level), - shouldConnectTwitter: !hasTwitter, - twitterShareUrl: hasTwitter ? `/api/social/share-x` : null, - }, - }); - } catch (error) { - console.error('Error claiming reward:', error); - res.status(500).json({ - success: false, - message: 'Server error', - }); + if (!user) { + res.status(404).json({ + success: false, + message: "User not found", + }); + return; } -}; \ No newline at end of file + + const hasTwitter = !!user.twitterHandle; + + await db + .update(users) + .set({ + impactLevel: getImpactLevel(level), + updatedAt: new Date(), + }) + .where(eq(users.id, userId)); + + const congratulatoryMessage = getCongratulatoryMessage(level); + + res.status(200).json({ + success: true, + data: { + numericLevel: level, + impactLevel: getImpactLevel(level), + shouldConnectTwitter: !hasTwitter, + twitterShareUrl: hasTwitter ? `/api/social/share-x` : null, + message: congratulatoryMessage, + }, + }); + } catch (error) { + console.error("Error claiming reward:", error); + res.status(500).json({ + success: false, + message: "Server error", + }); + } +}; diff --git a/src/routes/rewards.routes.ts b/src/routes/rewards.routes.ts index dd8e422..67098b2 100644 --- a/src/routes/rewards.routes.ts +++ b/src/routes/rewards.routes.ts @@ -1,9 +1,9 @@ import { Router } from 'express'; import { claimReward } from '../controllers/rewards.controller'; -import { authenticate } from '../middleware/auth.middleware'; +import { requireAuth } from '../middleware/auth.middleware'; const router = Router(); -router.post('/claim', authenticate, claimReward); +router.post('/claim', requireAuth, claimReward); export default router; \ No newline at end of file diff --git a/src/routes/social.routes.ts b/src/routes/social.routes.ts index 8812804..6ac584d 100644 --- a/src/routes/social.routes.ts +++ b/src/routes/social.routes.ts @@ -1,12 +1,12 @@ import { Router } from 'express'; import { connectX, shareX } from '../controllers/social.controller'; -import { authenticate } from '../middleware/auth.middleware'; +import { requireAuth } from '../middleware/auth.middleware'; const router = Router(); // Twitter OAuth routes -router.get('/connect-x', authenticate, connectX); +router.get('/connect-x', requireAuth, connectX); router.get('/connect-x/callback', connectX); -router.post('/share-x', authenticate, shareX); +router.post('/share-x', requireAuth, shareX); export default router; \ No newline at end of file