|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import * as achievementService from "../services/achievement.service"; |
| 3 | +import { uploadImage } from "../utils/imageUtils"; |
| 4 | +import { supabase } from "../app"; |
| 5 | +import { ApiError } from "../utils/apiError"; |
| 6 | + |
| 7 | +export const getAchievements = async (req: Request, res: Response) => { |
| 8 | + const achievements = await achievementService.getAchievements(); |
| 9 | + |
| 10 | + res.status(200).json({ |
| 11 | + success: true, |
| 12 | + count: achievements.length, |
| 13 | + data: achievements, |
| 14 | + }); |
| 15 | +}; |
| 16 | + |
| 17 | + |
| 18 | +export const getAchievementById = async (req: Request, res: Response) => { |
| 19 | + const achievementId = parseInt(req.params.achievementId); |
| 20 | + |
| 21 | + if (!achievementId || isNaN(achievementId)) { |
| 22 | + throw new ApiError("Invalid achievement ID", 400); |
| 23 | + } |
| 24 | + |
| 25 | + const achievement = await achievementService.getAchievementById(achievementId); |
| 26 | + |
| 27 | + if (!achievement) { |
| 28 | + throw new ApiError("Achievement not found", 404); |
| 29 | + } |
| 30 | + |
| 31 | + res.status(200).json({ |
| 32 | + success: true, |
| 33 | + data: achievement, |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +export const createAchievement = async (req: Request, res: Response) => { |
| 38 | + const file = req.file; |
| 39 | + if (!file) { |
| 40 | + throw new ApiError('Image file is not found', 400); |
| 41 | + } |
| 42 | + |
| 43 | + const imageUrl = await uploadImage(supabase, file, 'achievements'); |
| 44 | + if (!imageUrl) { |
| 45 | + throw new ApiError('Image URL is missing', 400); |
| 46 | + } |
| 47 | + |
| 48 | + const { title, description, achievedAt, memberIds, createdById } = req.body.achievementData; |
| 49 | + |
| 50 | + if (!title || !description || !achievedAt || !createdById) { |
| 51 | + throw new ApiError('Title, description, achievedAt, and createdById are required', 400); |
| 52 | + } |
| 53 | + |
| 54 | + if (!Array.isArray(memberIds)) { |
| 55 | + throw new ApiError('memberIds must be an array', 400); |
| 56 | + } |
| 57 | + |
| 58 | + const achievement = await achievementService.createAchievement({ |
| 59 | + title, |
| 60 | + description, |
| 61 | + achievedAt, |
| 62 | + imageUrl, |
| 63 | + memberIds, |
| 64 | + createdById, |
| 65 | + }); |
| 66 | + |
| 67 | + res.status(201).json({ |
| 68 | + success: true, |
| 69 | + data: achievement, |
| 70 | + }); |
| 71 | +}; |
| 72 | + |
| 73 | + |
| 74 | +export const updateAchievementById = async (req: Request, res: Response) => { |
| 75 | + const achievementId = parseInt(req.params.achievementId); |
| 76 | + if (!achievementId || isNaN(achievementId)) { |
| 77 | + throw new ApiError("Invalid achievement ID", 400); |
| 78 | + } |
| 79 | + |
| 80 | + const file = req.file; |
| 81 | + let imageUrl: string | undefined; |
| 82 | + |
| 83 | + if (file) { |
| 84 | + imageUrl = await uploadImage(supabase, file, 'achievements'); |
| 85 | + } |
| 86 | + |
| 87 | + let achievementData = req.body.achievementData; |
| 88 | + if (typeof achievementData === 'string') { |
| 89 | + try { |
| 90 | + achievementData = JSON.parse(achievementData); |
| 91 | + } catch (e) { |
| 92 | + throw new ApiError("Invalid JSON in achievementData field", 400); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const { title, description, achievedAt, updatedById, memberIds } = achievementData; |
| 97 | + |
| 98 | + if (!updatedById) { |
| 99 | + throw new ApiError("updatedById is required", 400); |
| 100 | + } |
| 101 | + |
| 102 | + if ( |
| 103 | + !title && |
| 104 | + !description && |
| 105 | + !achievedAt && |
| 106 | + !imageUrl && |
| 107 | + (!Array.isArray(memberIds) || memberIds.length === 0) |
| 108 | + ) { |
| 109 | + throw new ApiError("At least one field must be provided for update", 400); |
| 110 | + } |
| 111 | + |
| 112 | + const existingAchievement = await achievementService.getAchievementById(achievementId); |
| 113 | + if (!existingAchievement) { |
| 114 | + throw new ApiError("Achievement not found", 404); |
| 115 | + } |
| 116 | + |
| 117 | + if (imageUrl) { |
| 118 | + achievementData.imageUrl = imageUrl; |
| 119 | + } |
| 120 | + |
| 121 | + const updatedAchievement = await achievementService.updateAchievementById( |
| 122 | + achievementId, |
| 123 | + achievementData |
| 124 | + ); |
| 125 | + |
| 126 | + if (Array.isArray(memberIds) && memberIds.length > 0) { |
| 127 | + await achievementService.addMembersToAchievement(achievementId, memberIds); |
| 128 | + } |
| 129 | + |
| 130 | + res.status(200).json({ |
| 131 | + success: true, |
| 132 | + data: updatedAchievement, |
| 133 | + }); |
| 134 | +}; |
| 135 | + |
| 136 | + |
| 137 | +export const deleteAchievementById = async (req: Request, res: Response) => { |
| 138 | + const achievementId = parseInt(req.params.achievementId); |
| 139 | + |
| 140 | + if (!achievementId || isNaN(achievementId)) { |
| 141 | + throw new ApiError("Invalid achievement ID", 400); |
| 142 | + } |
| 143 | + |
| 144 | + await achievementService.deleteAchievementById(achievementId); |
| 145 | + |
| 146 | + res.status(200).json({ |
| 147 | + success: true, |
| 148 | + message: "Achievement deleted successfully", |
| 149 | + }); |
| 150 | +}; |
| 151 | + |
| 152 | + |
| 153 | +export const removeMemberFromAchievement = async (req: Request, res: Response) => { |
| 154 | + const achievementId = parseInt(req.params.achievementId); |
| 155 | + const memberId = req.params.memberId; |
| 156 | + |
| 157 | + if (!achievementId || isNaN(achievementId)) { |
| 158 | + throw new ApiError("Invalid achievement ID", 400); |
| 159 | + } |
| 160 | + |
| 161 | + if (!memberId) { |
| 162 | + throw new ApiError("Member ID is required", 400); |
| 163 | + } |
| 164 | + |
| 165 | + await achievementService.removeMemberFromAchievement(achievementId, memberId); |
| 166 | + |
| 167 | + res.status(200).json({ |
| 168 | + success: true, |
| 169 | + message: "Member removed from achievement successfully", |
| 170 | + }); |
| 171 | +}; |
| 172 | + |
0 commit comments