diff --git a/src/mailer/mail-templates/tempUserRegistration.ejs b/src/mailer/mail-templates/tempUserRegistration.ejs index 59e46a0b6678dae30977d9ca44dd514052a00526..0b318c49926d53013ab226c4d0cdb5c61c9d16a9 100644 --- a/src/mailer/mail-templates/tempUserRegistration.ejs +++ b/src/mailer/mail-templates/tempUserRegistration.ejs @@ -2,7 +2,7 @@ Bonjour,<br /> <br /> Vous recevez ce message car vous avez été relié à la stucture <strong><%= name %></strong> sur RES'in, le réseau des acteurs de l'inclusion numérique de la Métropole de Lyon. Vous pouvez dès maitenant vous créer un compte sur la -plateforme pour accéder a votre structure en -<a href="<%= config.protocol %>://<%= config.host %><%= config.port ? ':' + config.port : '' %>/register?id=<%= id %>" +plateforme pour accéder à votre structure en +<a href="<%= config.protocol %>://<%= config.host %><%= config.port ? ':' + config.port : '' %>/form/register/<%= id %>" >cliquant ici</a >. diff --git a/src/structures/structures.controller.spec.ts b/src/structures/structures.controller.spec.ts index d11767f4127052f4b5aabf8e1e153633dec29dc1..2e77f0918cf317f90fa45d6c8123f50e5859a8ab 100644 --- a/src/structures/structures.controller.spec.ts +++ b/src/structures/structures.controller.spec.ts @@ -195,6 +195,11 @@ describe('AuthController', () => { expect(res).toBeTruthy(); }); + it('should remove Owner', async () => { + const res = controller.removeTempUser('6093ba0e2ab5775cfc01ed3e', 'tsfsf6296'); + expect(res).toBeTruthy(); + }); + it('should join user', async () => { const userMock = new UsersServiceMock(); const user = userMock.findOne('pauline.dupont@mii.com'); diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 5cf6f71cef4074a2e7bc5dd4a57d2b052fb2d579..d62bf74ab34f8c76fab5ee7a6d43361b49ccbec6 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -152,6 +152,11 @@ export class StructuresController { return this.structureService.findWithOwners(id, data.emailUser); } + @Get(':id/tempUsers') + public async getTempUsers(@Param('id') id: string) { + return this.tempUserService.getStructureTempUsers(id); + } + @Delete(':id') @UseGuards(JwtAuthGuard, IsStructureOwnerGuard) @Roles('admin') @@ -203,7 +208,6 @@ export class StructuresController { @Delete(':id/owner/:userId') @UseGuards(JwtAuthGuard, IsStructureOwnerGuard) - @Roles('admin') @ApiParam({ name: 'id', type: String, required: true }) @ApiParam({ name: 'userId', type: String, required: true }) public async removeOwner(@Param('id') id: string, @Param('userId') userId: string): Promise<void> { @@ -220,6 +224,24 @@ export class StructuresController { this.userService.removeFromStructureLinked(userFromDb.email, id); } + @Delete(':id/tempUser/:userId') + @UseGuards(JwtAuthGuard, IsStructureOwnerGuard) + @ApiParam({ name: 'id', type: String, required: true }) + @ApiParam({ name: 'userId', type: String, required: true }) + public async removeTempUser(@Param('id') id: string, @Param('userId') userId: string): Promise<void> { + // Get structure + const structure = await this.structureService.findOne(id); + if (!structure) { + throw new HttpException('Invalid Structure', HttpStatus.NOT_FOUND); + } + // Get temp user + const userFromDb = await this.tempUserService.findById(userId); + if (!userFromDb || !userFromDb.pendingStructuresLink.includes(Types.ObjectId(id))) { + throw new HttpException('Invalid temp user', HttpStatus.NOT_FOUND); + } + this.tempUserService.removeFromStructureLinked(userFromDb.email, id); + } + @Post('reportStructureError') public async reportStructureError(@Body() data: { structureId: string; content: string }): Promise<void> { return this.structureService.reportStructureError(data.structureId, data.content); diff --git a/src/temp-user/temp-user.controller.spec.ts b/src/temp-user/temp-user.controller.spec.ts index 37e355b9781e054fcf5bc81b84a2d363c2152cbe..ca61b3452d6b7671cce3c78412ec851d9c9eb24b 100644 --- a/src/temp-user/temp-user.controller.spec.ts +++ b/src/temp-user/temp-user.controller.spec.ts @@ -40,7 +40,7 @@ describe('TempUserService', () => { // Fail test if above expression doesn't throw anything. expect(true).toBe(false); } catch (e) { - expect(e.message).toEqual('User does not exists'); + expect(e.message).toEqual('Temp user does not exists'); expect(e.status).toEqual(HttpStatus.BAD_REQUEST); } }); diff --git a/src/temp-user/temp-user.controller.ts b/src/temp-user/temp-user.controller.ts index 9dc21a559e179a4ba1fc59f6c70ff7b6e9a7dc01..bfdc230bf39083a706d2d34fd408159ac9f9c848 100644 --- a/src/temp-user/temp-user.controller.ts +++ b/src/temp-user/temp-user.controller.ts @@ -12,7 +12,7 @@ export class TempUserController { public async getTempUser(@Param('id') id: string): Promise<TempUser> { const user = await this.tempUserSercice.findById(id); if (!user) { - throw new HttpException('User does not exists', HttpStatus.BAD_REQUEST); + throw new HttpException('Temp user does not exists', HttpStatus.BAD_REQUEST); } return user; } diff --git a/src/temp-user/temp-user.service.ts b/src/temp-user/temp-user.service.ts index 8a43832237b1555687568f84cc92b2f877858336..da41551c5a04e0a950d507dfc216e78062924a74 100644 --- a/src/temp-user/temp-user.service.ts +++ b/src/temp-user/temp-user.service.ts @@ -25,7 +25,7 @@ export class TempUserService { return this.findOne(createTempUser.email); } - public async findOne(mail: string): Promise<TempUser> { + public async findOne(mail: string): Promise<ITempUser> { return this.tempUserModel.findOne({ email: mail }); } @@ -38,7 +38,7 @@ export class TempUserService { if (!userInDb) { throw new HttpException('User does not exists', HttpStatus.BAD_REQUEST); } - this.tempUserModel.deleteOne({ email: mail }); + await this.tempUserModel.deleteOne({ email: mail }); return userInDb; } @@ -82,4 +82,27 @@ export class TempUserService { }); this.mailerService.send(user.email, jsonConfig.subject, html); } + + public async getStructureTempUsers(structureId: string): Promise<ITempUser[]> { + const tempUsers = await this.tempUserModel + .find({ pendingStructuresLink: Types.ObjectId(structureId) }) + .select('email updatedAt') + .exec(); + return tempUsers; + } + + public async removeFromStructureLinked(userEmail: string, idStructure: string): Promise<Types.ObjectId[]> { + const user = await this.findOne(userEmail); + if (!user) { + throw new HttpException('Invalid temp user', HttpStatus.NOT_FOUND); + } + if (!user.pendingStructuresLink.includes(Types.ObjectId(idStructure))) { + throw new HttpException("Temp user doesn't belong to this structure", HttpStatus.NOT_FOUND); + } + user.pendingStructuresLink = user.pendingStructuresLink.filter((structureId) => { + return !structureId.equals(idStructure); + }); + await user.save(); + return user.pendingStructuresLink; + } } diff --git a/src/users/services/userRegistry.service.spec.ts b/src/users/services/userRegistry.service.spec.ts index 0df3a840e63bbdc962004ba3b2c557a5b2361d64..87eafc904a07506cf9d1b86cadf1de3bd345e086 100644 --- a/src/users/services/userRegistry.service.spec.ts +++ b/src/users/services/userRegistry.service.spec.ts @@ -14,6 +14,8 @@ describe('userRegistryService', () => { let service: UserRegistryService; const mockUserRegistryModel = { find: jest.fn(() => mockUserRegistryModel), + where: jest.fn(() => mockUserRegistryModel), + equals: jest.fn(() => mockUserRegistryModel), populate: jest.fn(() => mockUserRegistryModel), sort: jest.fn(() => mockUserRegistryModel), select: jest.fn(() => mockUserRegistryModel), diff --git a/src/users/services/userRegistry.service.ts b/src/users/services/userRegistry.service.ts index 941dd2daabb00a59bbbf110e8d1953807cff6b33..8f94a12a042a24c1baf0994d6dcf0428b6af229e 100644 --- a/src/users/services/userRegistry.service.ts +++ b/src/users/services/userRegistry.service.ts @@ -24,6 +24,8 @@ export class UserRegistryService { public async findAllForIndexation(): Promise<IUserRegistry[]> { return this.userModel .find() + .where('emailVerified') + .equals(true) .select('name surname _id job employer ') .populate('job employer') .sort({ surname: 1 }) @@ -33,6 +35,8 @@ export class UserRegistryService { public async countAllUserRegistry(): Promise<number> { return this.userModel .find() + .where('emailVerified') + .equals(true) .populate('employer') .populate('job') .select('name surname employer job _id ') @@ -46,6 +50,8 @@ export class UserRegistryService { const count = await this.countAllUserRegistry(); const docs = await this.userModel .find() + .where('emailVerified') + .equals(true) .populate('employer') .populate('job') .select('name surname employer job _id ') @@ -68,7 +74,6 @@ export class UserRegistryService { employersNames.findIndex((n) => user.employer?.name === n) > -1 ); } - if (employersList?.length) { return users.filter((user) => employersNames.findIndex((n) => user.employer?.name === n) > -1); } @@ -109,6 +114,8 @@ export class UserRegistryService { }, ], }) + .where('emailVerified') + .equals(true) .select('name surname employer job _id ') .populate('employer job') .sort({ surname: 1 }) diff --git a/src/users/services/users.service.ts b/src/users/services/users.service.ts index 19c9d58b63c09a3e9eea3cf41f384c44b0b913fa..f3d03ae2ad7a7a3b74d46883656004ce170b0450 100644 --- a/src/users/services/users.service.ts +++ b/src/users/services/users.service.ts @@ -18,8 +18,8 @@ import { JobDocument } from '../schemas/job.schema'; import { PersonalOfferDocument } from '../../personal-offers/schemas/personal-offer.schema'; import { UpdateDetailsDto } from '../dto/update-details.dto'; import { DescriptionDto } from '../dto/description.dto'; -import { UserRegistrySearchService } from './userRegistry-search.service'; import { IUserRegistry } from '../interfaces/userRegistry.interface'; +import { UserRegistrySearchService } from './userRegistry-search.service'; @Injectable() export class UsersService { @@ -550,17 +550,17 @@ export class UsersService { public async removeFromStructureLinked(userEmail: string, idStructure: string): Promise<Types.ObjectId[]> { const user = await this.findOne(userEmail, true); - if (user) { - if (user.structuresLink.includes(Types.ObjectId(idStructure))) { - user.structuresLink = user.structuresLink.filter((structureId) => { - return !structureId.equals(idStructure); - }); - await user.save(); - return user.structuresLink; - } - throw new HttpException('User already belong to this structure', HttpStatus.NOT_FOUND); + if (!user) { + throw new HttpException('Invalid user', HttpStatus.NOT_FOUND); } - throw new HttpException('Invalid user', HttpStatus.NOT_FOUND); + if (!user.structuresLink.includes(Types.ObjectId(idStructure))) { + throw new HttpException("User doesn't belong to this structure", HttpStatus.NOT_FOUND); + } + user.structuresLink = user.structuresLink.filter((structureId) => { + return !structureId.equals(idStructure); + }); + await user.save(); + return user.structuresLink; } /**