diff --git a/api/src/controllers/userController.ts b/api/src/controllers/userController.ts index c1e7e8a3c..4f3df8784 100644 --- a/api/src/controllers/userController.ts +++ b/api/src/controllers/userController.ts @@ -1434,7 +1434,7 @@ export const deleteUsers = async (req: Request, res: Response) => { } } - if (user.contracts) { + if (user.contracts && user.contracts.length > 0) { for (const contract of user.contracts) { if (contract.file) { const file = path.join(env.CDN_CONTRACTS, contract.file) diff --git a/api/tests/user.test.ts b/api/tests/user.test.ts index b141a04c9..0e1e87fab 100644 --- a/api/tests/user.test.ts +++ b/api/tests/user.test.ts @@ -1290,12 +1290,24 @@ describe('POST /api/delete-users', () => { it('should delete users', async () => { const token = await testHelper.signinAsAdmin() - let payload: string[] = [USER1_ID, USER2_ID, ADMIN_ID] + const supplierName1 = testHelper.getSupplierName() + const supplier1Id = await testHelper.createSupplier(`${supplierName1}@test.bookcars.ma`, supplierName1) + + const supplierName2 = testHelper.getSupplierName() + const supplier2Id = await testHelper.createSupplier(`${supplierName2}@test.bookcars.ma`, supplierName1) + const supplier2 = await User.findById(supplier2Id) + supplier2!.contracts = [ + { language: 'en', file: null }, + { language: 'fr', file: `${nanoid()}.pdf` }, + ] + await supplier2?.save() + + let payload: string[] = [USER1_ID, USER2_ID, ADMIN_ID, supplier1Id, supplier2Id] const user1 = await User.findById(USER1_ID) user1!.avatar = `${nanoid()}.jpg` await user1?.save() let users = await User.find({ _id: { $in: payload } }) - expect(users.length).toBe(3) + expect(users.length).toBe(5) let res = await request(app) .post('/api/delete-users') .set(env.X_ACCESS_TOKEN, token)