Skip to content

Commit

Permalink
Merge pull request #95 from atlp-rwanda/fix-stock-quantity-verify-user
Browse files Browse the repository at this point in the history
fix: stock quantity update and verify user
  • Loading branch information
teerenzo authored Jul 24, 2024
2 parents e2bab33 + 6d51509 commit 07072e9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
12 changes: 6 additions & 6 deletions __test__/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ describe("Testing product Routes", () => {
const token = generateVerificationToken('[email protected]', 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 () =>{
Expand All @@ -148,8 +148,8 @@ describe("Testing product Routes", () => {
const token = generateVerificationToken('[email protected]', 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({
Expand All @@ -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 () => {
Expand Down
27 changes: 15 additions & 12 deletions __test__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -213,9 +214,10 @@ describe("Testing user Routes", () => {
it("It should verify user account.",async()=>{
const token = generateVerificationToken('[email protected]', 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 () =>{
Expand All @@ -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)
Expand Down Expand Up @@ -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()=>{
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.' });
}
Expand Down
3 changes: 3 additions & 0 deletions src/services/OrderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

5 changes: 3 additions & 2 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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.' };
}
Expand Down

0 comments on commit 07072e9

Please sign in to comment.