Skip to content

Commit a7e44d7

Browse files
committed
Some fixes in update acheivement controller
1 parent df481bd commit a7e44d7

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

src/controllers/achievement.controller.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const createAchievement = async (req: Request, res: Response) => {
7373

7474
export const updateAchievementById = async (req: Request, res: Response) => {
7575
const achievementId = parseInt(req.params.achievementId);
76+
7677
if (!achievementId || isNaN(achievementId)) {
7778
throw new ApiError("Invalid achievement ID", 400);
7879
}
@@ -81,7 +82,7 @@ export const updateAchievementById = async (req: Request, res: Response) => {
8182
let imageUrl: string | undefined;
8283

8384
let achievementData = req.body.achievementData;
84-
if (typeof achievementData === 'string') {
85+
if (typeof achievementData === "string") {
8586
try {
8687
achievementData = JSON.parse(achievementData);
8788
} catch (e) {
@@ -95,33 +96,31 @@ export const updateAchievementById = async (req: Request, res: Response) => {
9596
throw new ApiError("updatedById is required", 400);
9697
}
9798

98-
9999
const existingAchievement = await achievementService.getAchievementById(achievementId);
100100
if (!existingAchievement) {
101101
throw new ApiError("Achievement not found", 404);
102102
}
103-
103+
104104
if (file) {
105-
imageUrl = await uploadImage(supabase, file, 'achievements', existingAchievement.imageUrl );
105+
imageUrl = await uploadImage(supabase, file, "achievements", existingAchievement.imageUrl);
106106
}
107-
107+
108+
const { memberIds: _, ...updatePayload } = achievementData;
109+
108110
if (imageUrl) {
109-
achievementData.imageUrl = imageUrl;
110-
}
111-
112-
if (
113-
!title &&
114-
!description &&
115-
!achievedAt &&
116-
!imageUrl &&
117-
(!Array.isArray(memberIds) || memberIds.length === 0)
118-
) {
119-
throw new ApiError("At least one field must be provided for update", 400);
120-
}
121-
111+
updatePayload.imageUrl = imageUrl;
112+
}
113+
114+
const hasSomethingToUpdate =
115+
title || description || achievedAt || imageUrl || (Array.isArray(memberIds) && memberIds.length > 0);
116+
117+
if (!hasSomethingToUpdate) {
118+
throw new ApiError("At least one field (title, description, achievedAt, image, or memberIds) must be provided for update", 400);
119+
}
120+
122121
const updatedAchievement = await achievementService.updateAchievementById(
123122
achievementId,
124-
achievementData
123+
updatePayload
125124
);
126125

127126
if (Array.isArray(memberIds) && memberIds.length > 0) {

0 commit comments

Comments
 (0)