From 6d51509e7d0ace3f88b0bfe28b381d18e4c3adb9 Mon Sep 17 00:00:00 2001 From: yvanddniyo Date: Tue, 23 Jul 2024 15:49:16 +0200 Subject: [PATCH] fix: stock quantity update and verify user - seller when marked as delivery should decrease the quantity of the product - when user sign up should get verification link and be able to navigate to login - when order delivery should remove the amount of the quantiy purchased [No Id] --- __test__/product.test.ts | 12 ++++++------ __test__/user.test.ts | 27 +++++++++++++++------------ src/controllers/userControllers.ts | 8 ++++++-- src/services/OrderService.ts | 3 +++ src/services/user.service.ts | 5 +++-- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/__test__/product.test.ts b/__test__/product.test.ts index df126e4..a68cc5e 100644 --- a/__test__/product.test.ts +++ b/__test__/product.test.ts @@ -124,8 +124,8 @@ describe("Testing product Routes", () => { const token = generateVerificationToken('soleil@soleil0w.com', 60); const response = await request(app) .get(`/api/v1/users/verify-user?token=${token}`) - expect(response.status).toBe(200) - expect(response.body.message).toBe('User verified successfully.') + expect(response.status).toBe(302); + expect(response.headers.location).toBe(`${process.env.FE_URL}/verify-user`); },60000) test("should login an buyer", async () =>{ @@ -148,8 +148,8 @@ describe("Testing product Routes", () => { const token = generateVerificationToken('admin1@example.com', 60); const response = await request(app) .get(`/api/v1/users/verify-user?token=${token}`) - expect(response.status).toBe(200) - expect(response.body.message).toBe('User verified successfully.') + expect(response.status).toBe(302); + expect(response.headers.location).toBe(`${process.env.FE_URL}/verify-user`); },60000) test("should login an Admin", async () =>{ const response = await request(app).post("/api/v1/users/login").send({ @@ -163,8 +163,8 @@ describe("Testing product Routes", () => { const token = generateVerificationToken(dummySeller.email, 60); const response = await request(app) .get(`/api/v1/users/verify-user?token=${token}`) - expect(response.status).toBe(200) - expect(response.body.message).toBe('User verified successfully.') + expect(response.status).toBe(302); + expect(response.headers.location).toBe(`${process.env.FE_URL}/verify-user`); },60000) test("should update dummyseller's role to seller", async () => { diff --git a/__test__/user.test.ts b/__test__/user.test.ts index 2020c8e..6fde2d3 100644 --- a/__test__/user.test.ts +++ b/__test__/user.test.ts @@ -118,9 +118,10 @@ describe("Testing user Routes", () => { it("It should verify user account.",async()=>{ const token = generateVerificationToken(userData.email, 60); const response = await request(app) - .get(`/api/v1/users/verify-user?token=${token}`) - expect(response.status).toBe(200) - expect(response.body.message).toBe('User verified successfully.') + .get(`/api/v1/users/verify-user?token=${token}`) + expect(response.status).toBe(302); + expect(response.headers.location).toBe(`${process.env.FE_URL}/verify-user`); + },60000) @@ -213,9 +214,10 @@ describe("Testing user Routes", () => { it("It should verify user account.",async()=>{ const token = generateVerificationToken('admin1@example.com', 60); const response = await request(app) - .get(`/api/v1/users/verify-user?token=${token}`) - expect(response.status).toBe(200) - expect(response.body.message).toBe('User verified successfully.') + .get(`/api/v1/users/verify-user?token=${token}`) + expect(response.status).toBe(302); + expect(response.headers.location).toBe(`${process.env.FE_URL}/verify-user`); + },60000) test("should login an Admin", async () =>{ @@ -229,9 +231,9 @@ describe("Testing user Routes", () => { const token = generateVerificationToken(dummySeller.email, 60); const response = await request(app) .get(`/api/v1/users/verify-user?token=${token}`) - expect(response.status).toBe(200) - expect(response.body.message).toBe('User verified successfully.') - },60000) + expect(response.status).toBe(302); + expect(response.headers.location).toBe(`${process.env.FE_URL}/verify-user`); + },60000) test("should return 200 when all roles are fetched", async () => { const response = await request(app) @@ -591,9 +593,10 @@ describe("Verifying user account",()=>{ await User.create(userData) const token = generateVerificationToken(userData.email, 60); const response = await request(app) - .get(`/api/v1/users/verify-user?token=${token}`) - expect(response.status).toBe(200) - expect(response.body.message).toBe('User verified successfully.') + .get(`/api/v1/users/verify-user?token=${token}`) + expect(response.status).toBe(302); + expect(response.headers.location).toBe(`${process.env.FE_URL}/verify-user`); + },60000) it("It should send a verification link.",async()=>{ diff --git a/src/controllers/userControllers.ts b/src/controllers/userControllers.ts index 9965bae..be1a723 100644 --- a/src/controllers/userControllers.ts +++ b/src/controllers/userControllers.ts @@ -70,7 +70,7 @@ export const userLogin = async (req: Request, res: Response) => { if (!user || user === null) { res.status(404).json({ status: 404, - message: "User Not Found ! Please Register new ancount", + message: "Invalid credentials!", }); } else { accessToken = await generateToken(user); @@ -462,7 +462,11 @@ export const verifyUserController = async (req: Request, res: Response): Promise // Extract token from the request query const token = req.query.token as string; const result = await userService.verifyNewUser(token); - res.status(result.status).json({ message: result.message }); + if (result.status === 200) { + res.redirect(result.redirectLogin); + } else { + res.status(result.status).json({ message: result.message }); + } } catch (error) { res.status(500).json({ error: 'Failed to verify new user.' }); } diff --git a/src/services/OrderService.ts b/src/services/OrderService.ts index 60743d9..9f333e2 100644 --- a/src/services/OrderService.ts +++ b/src/services/OrderService.ts @@ -96,9 +96,12 @@ export const updateOrderStatusService = async (userId: string, orderId: string, if (!orderItems.length) { throw new Error("Order items not found or not associated with this seller"); } + for (const item of orderItems) { item.status = status; await item.save(); } + return orderItems; }; + diff --git a/src/services/user.service.ts b/src/services/user.service.ts index 02587f6..8f9f609 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -115,7 +115,7 @@ export const createUserService = async (name: string, email: string, username: s const subject = 'Please verify your email address'; const token = generateVerificationToken(user.email, 60); - const verificationLink = `${process.env.REMOTE_URL || `${process.env.LOCAL_URL}:${process.env.PORT}`}/api/v1/users/verify-user?token=${token}`; + const verificationLink = `${process.env.REMOTE_URL || process.env.LOCAL_URL}/api/v1/users/verify-user?token=${token}`; await sendEmailService(user,subject,verifyUserEmailTemplate(user.username,verificationLink)) @@ -310,7 +310,8 @@ export const verifyNewUser = async (token: string) => { await addToBlacklist(token); if (updatedRows > 0) { await sendEmailService(user,subject,generateEmailVerificationEmail(decodedToken.email)) - return { status: 200, message: 'User verified successfully.' }; + const redirectLogin:any = `${process.env.FE_URL}/verify-user`; + return { status: 200, redirectLogin }; } else { return { status: 404, message: 'User not found or already verified.' }; }