Skip to content

Commit

Permalink
fix(200 response): change all responses with status 200 to JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
quannhg committed Dec 19, 2023
1 parent cac4002 commit ebf514c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/handlers/auth.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const logout: Handler = async (_req, res) => {
return null;
};

const verifyOTP: Handler<string, { Params: { userId: string }; Body: VerifyOTPInputDto }> = async (req, res) => {
const verifyOTP: Handler<{ message: string }, { Params: { userId: string }; Body: VerifyOTPInputDto }> = async (req, res) => {
const verificationEmail = await prisma.verificationEmail.findFirst({
select: {
expiration_time: true
Expand Down Expand Up @@ -142,10 +142,10 @@ const verifyOTP: Handler<string, { Params: { userId: string }; Body: VerifyOTPIn
}
});

return 'Verify successfully !';
return { message: 'Verify successfully !' };
};

const sendOTP: Handler<string, { Params: { userId: string } }> = async (req, res) => {
const sendOTP: Handler<{ message: string }, { Params: { userId: string } }> = async (req, res) => {
const { userId } = req.params;
const user = await prisma.user.findUnique({
select: {
Expand Down Expand Up @@ -196,7 +196,7 @@ const sendOTP: Handler<string, { Params: { userId: string } }> = async (req, res
html: `<p>Enter this OTP to verify your tick3d account: ${otp}</p>`
});

return 'Verification email sent';
return { message: 'Verification email sent' };
};

export const authHandler = {
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/cart.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const get: Handler<GetCartResultDto> = async (req, res) => {
}
};

const add: Handler<string, { Body: AddCartInputDto }> = async (req, res) => {
const add: Handler<{ message: string }, { Body: AddCartInputDto }> = async (req, res) => {
const user_id = req.userId;
const { models } = req.body;

Expand Down Expand Up @@ -75,7 +75,7 @@ const add: Handler<string, { Body: AddCartInputDto }> = async (req, res) => {
)
);
});
return res.send('Added successfully');
return res.send({ message: 'Added successfully' });
} catch (e) {
console.error('Error adding to cart:', e);

Expand All @@ -87,7 +87,7 @@ const add: Handler<string, { Body: AddCartInputDto }> = async (req, res) => {
}
};

const del: Handler<string, { Body: DelCartInputDto }> = async (req, res) => {
const del: Handler<{ message: string }, { Body: DelCartInputDto }> = async (req, res) => {
const user_id = req.userId;
const { models } = req.body;

Expand All @@ -105,13 +105,13 @@ const del: Handler<string, { Body: DelCartInputDto }> = async (req, res) => {
)
);

return 'Deleted successfully';
return { message: 'Deleted successfully' };
} catch (e) {
return res.badRequest(DELETE_CART_FAILED);
}
};

const delAll: Handler<string> = async (req) => {
const delAll: Handler<{ message: string }> = async (req) => {
const user_id = req.userId;

await prisma.cart.deleteMany({
Expand All @@ -120,7 +120,7 @@ const delAll: Handler<string> = async (req) => {
}
});

return 'Reset cart successfully';
return { message: 'Reset cart successfully' };
};

export const cartHandler = {
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/defaultModel.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const upload: Handler<DefaultModelListResultDto, { Body: UploadDefaultModelInput
return outputList;
};

const del: Handler<string, { Params: { id: string } }> = async (req, res) => {
const del: Handler<{ message: string }, { Params: { id: string } }> = async (req, res) => {
const { id } = req.params;

try {
Expand All @@ -226,10 +226,10 @@ const del: Handler<string, { Params: { id: string } }> = async (req, res) => {
return res.internalServerError(DELETE_MODEL_FAILED);
}

return 'Delete successfully';
return { message: 'Delete successfully' };
};

const update: Handler<string, { Params: { id: string }; Body: UpdateDefaultModelInputDto }> = async (req, res) => {
const update: Handler<{ message: string }, { Params: { id: string }; Body: UpdateDefaultModelInputDto }> = async (req, res) => {
const { id } = req.params;
const { gcode, name, price, category_id, imageUrl, discount, description, subImageUrls } = req.body;
try {
Expand Down Expand Up @@ -264,7 +264,7 @@ const update: Handler<string, { Params: { id: string }; Body: UpdateDefaultModel
return res.internalServerError(UPDATE_MODEL_FAILED);
}

return 'Update successfully';
return { message: 'Update successfully' };
};

const toggleLike: Handler<ToggleLikeResultDto, { Params: { id: string } }> = async (req, res) => {
Expand Down Expand Up @@ -335,7 +335,7 @@ const toggleLike: Handler<ToggleLikeResultDto, { Params: { id: string } }> = asy
}
};

const discontinue: Handler<string, { Params: { id: string } }> = async (req, res) => {
const discontinue: Handler<{ message: string }, { Params: { id: string } }> = async (req, res) => {
const { id } = req.params;
try {
await prisma.$transaction(async (prisma) => {
Expand All @@ -360,7 +360,7 @@ const discontinue: Handler<string, { Params: { id: string } }> = async (req, res
return res.internalServerError('Model is discontinued failed');
}

return 'Discontinue successfully';
return { message: 'Discontinue successfully' };
};

export const defaultModelHandler = {
Expand Down
8 changes: 4 additions & 4 deletions src/handlers/userModel.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const upload: Handler<UserModelListResultDto, { Body: UploadUserModelInputDto }>
return outputList;
};

const del: Handler<string, { Params: { id: string } }> = async (req, res) => {
const del: Handler<{ message: string }, { Params: { id: string } }> = async (req, res) => {
const userId = req.userId;
const { id } = req.params;

Expand All @@ -162,10 +162,10 @@ const del: Handler<string, { Params: { id: string } }> = async (req, res) => {
return res.internalServerError(DELETE_MODEL_FAILED);
}

return 'Delete succesfully';
return { message: 'Delete succesfully' };
};

const update: Handler<string, { Params: { id: string }; Body: UpdateUserModelInputDto }> = async (req, res) => {
const update: Handler<{ message: string }, { Params: { id: string }; Body: UpdateUserModelInputDto }> = async (req, res) => {
const userId = req.userId;
const { id } = req.params;
const { gcode, name, price } = req.body;
Expand Down Expand Up @@ -193,7 +193,7 @@ const update: Handler<string, { Params: { id: string }; Body: UpdateUserModelInp
return res.internalServerError(UPDATE_MODEL_FAILED);
}

return 'Update succesfully';
return { message: 'Update succesfully' };
};

export const userModelHandler = {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/apis/cart.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const cartPlugin = createRoutes('Cart', [
summary: 'Add some models to the cart of the current user (based on jwt)',
body: AddCartInputDto,
response: {
200: Type.String(),
200: Type.Object({ message: Type.String() }),
400: Type.String()
}
},
Expand All @@ -40,7 +40,7 @@ export const cartPlugin = createRoutes('Cart', [
schema: {
summary: 'Reset the cart of the current user (based on jwt)',
response: {
200: Type.String()
200: Type.Object({ message: Type.String() })
}
},
handler: cartHandler.delAll
Expand All @@ -53,7 +53,7 @@ export const cartPlugin = createRoutes('Cart', [
summary: 'Remove some models from the cart of the current user (based on jwt)',
body: DelCartInputDto,
response: {
200: Type.String(),
200: Type.Object({ message: Type.String() }),
400: Type.String()
}
},
Expand Down
4 changes: 3 additions & 1 deletion src/routes/apis/defaultModel.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const defaultModelPlugin = createRoutes('Default Model', [
schema: {
summary: 'Delete a default model with the specified id',
description: 'Only the manager can do this',
response: { 200: Type.Object({ message: Type.String() }) },
params: {
id: Type.String()
}
Expand All @@ -72,6 +73,7 @@ export const defaultModelPlugin = createRoutes('Default Model', [
params: {
id: Type.String()
},
response: { 200: Type.Object({ message: Type.String() }) },
body: UpdateDefaultModelInputDto
},
handler: defaultModelHandler.update
Expand All @@ -98,7 +100,7 @@ export const defaultModelPlugin = createRoutes('Default Model', [
schema: {
summary: 'Mark Default Model as Discontinued. Also, remove it from all current user carts.',
params: {
id: Type.String()
id: Type.Object({ message: Type.String() })
},
response: {
200: Type.String()
Expand Down
2 changes: 2 additions & 0 deletions src/routes/apis/userModel.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const userModelPlugin = createRoutes('User Model', [
onRequest: [verifyToken],
schema: {
summary: 'Delete the user model with the specified id',
response: { 200: Type.Object({ message: Type.String() }) },
params: {
id: Type.String()
}
Expand All @@ -69,6 +70,7 @@ export const userModelPlugin = createRoutes('User Model', [
schema: {
summary: 'Update a user model',
description: 'Only the customer can do this',
response: { 200: Type.Object({ message: Type.String() }) },
params: {
id: Type.String()
},
Expand Down
4 changes: 2 additions & 2 deletions src/routes/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const authPlugin = createRoutes('Auth', [
},
body: VerifyOTPInputDto,
response: {
200: Type.String(),
200: Type.Object({ message: Type.String() }),
404: Type.Null()
}
},
Expand All @@ -62,7 +62,7 @@ export const authPlugin = createRoutes('Auth', [
userId: Type.String()
},
response: {
200: Type.String(),
200: Type.Object({ message: Type.String() }),
400: Type.String()
}
},
Expand Down

0 comments on commit ebf514c

Please sign in to comment.