diff --git a/src/groups/dao/groups.dao.ts b/src/groups/dao/groups.dao.ts index 5be786497c21f139923275e270f4e77863135364..52e31cc1ce8fa5428157a2d8cf4ad1f8a68052a3 100644 --- a/src/groups/dao/groups.dao.ts +++ b/src/groups/dao/groups.dao.ts @@ -107,6 +107,53 @@ export class GroupsDao { ); }); + findByIdAndUpdateManyByRole = ( + id: string, + role: string, + peopleIds: string[], + action: string, + ): Promise<Group | void> => + new Promise((resolve, reject) => { + let query = {}; + if (action === 'post') { + switch (role) { + case Roles.ROLE_RESPONSIBLE: + query = { $addToSet: { responsibles: { $each: peopleIds } } }; + break; + case Roles.ROLE_SECRETARY: + query = { $addToSet: { secretaries: { $each: peopleIds } } }; + break; + case Roles.ROLE_STUDENT: + query = { $addToSet: { students: { $each: peopleIds } } }; + break; + default: + reject(new BadRequestException('Bad role')); + } + } else if (action === 'delete') { + switch (role) { + case Roles.ROLE_RESPONSIBLE: + query = { $pull: { responsibles: { $in: peopleIds } } }; + break; + case Roles.ROLE_SECRETARY: + query = { $pull: { secretaries: { $in: peopleIds } } }; + break; + case Roles.ROLE_STUDENT: + query = { $pull: { students: { $in: peopleIds } } }; + break; + default: + reject(new BadRequestException('Bad role')); + } + } else reject(new BadRequestException('Unknown action')); + this._groupModel.findByIdAndUpdate( + id, + query, + { new: true }, + (err, value) => { + if (err) reject(err.message); + resolve(value); + }); + }); + findByIdAndRemove = (id: string): Promise<Group | void> => new Promise((resolve, reject) => { this._groupModel.findByIdAndDelete(id, {}, (err) => { diff --git a/src/groups/groups.controller.ts b/src/groups/groups.controller.ts index 18cf22ed944d7f882878a0df13a675d1fbed848c..625f5ccf2ffd15e77dd730d6b2a580289833ce1a 100644 --- a/src/groups/groups.controller.ts +++ b/src/groups/groups.controller.ts @@ -13,11 +13,17 @@ import { CreateGroupDto } from './dto/create-group.dto'; import { UpdateGroupDto } from './dto/update-group.dto'; import { GroupEntity } from './entities/group.entity'; import { GroupsService } from './groups.service'; +import { CreatePeopleDto } from 'src/people/dto/create-people.dto'; +import { PeopleService } from 'src/people/people.service'; +import { roleValue } from 'src/shared/Roles'; @Controller('groups') @UseInterceptors(HttpInterceptor) export class GroupsController { - constructor(private readonly _groupsService: GroupsService) {} + constructor( + private readonly _groupsService: GroupsService, + private readonly _peopleService: PeopleService, + ) {} @Get() findAll(): Promise<GroupEntity[] | void> { @@ -34,6 +40,26 @@ export class GroupsController { return this._groupsService.create(createGroupDto); } + @Post(':id/import/:role') + async importAsFile( + @Param() params: { id: string; role: string }, + @Body() people: CreatePeopleDto[], + ): Promise<GroupEntity | void> { + const peopleEntities = await this._peopleService.createMany( + people.map((person) => { + person.role = roleValue(params.role); + return person; + }), + ); + const peopleIds = peopleEntities.map((person) => person._id); + return this._groupsService.updateManyByRole( + params.id, + params.role, + peopleIds, + 'post', + ); + } + @Put(':id') update( @Param() params: { id: string }, diff --git a/src/groups/groups.module.ts b/src/groups/groups.module.ts index b8c3b67845ab5cae0b2b67166bcd743a88abd817..3614a8348c3d1d6cae399c3279c4bfb7f581ebfc 100644 --- a/src/groups/groups.module.ts +++ b/src/groups/groups.module.ts @@ -1,5 +1,8 @@ import { Module, Logger } from '@nestjs/common'; import { MongooseModule } from '@nestjs/mongoose'; +import { PeopleDao } from 'src/people/dao/people.dao'; +import { PeopleService } from 'src/people/people.service'; +import { People, PeopleSchema } from 'src/people/schemas/people.schema'; import { GroupsDao } from './dao/groups.dao'; import { GroupsController } from './groups.controller'; import { GroupsService } from './groups.service'; @@ -8,8 +11,9 @@ import { Group, GroupSchema } from './schemas/group.schema'; @Module({ imports: [ MongooseModule.forFeature([{ name: Group.name, schema: GroupSchema }]), + MongooseModule.forFeature([{ name: People.name, schema: PeopleSchema }]), ], controllers: [GroupsController], - providers: [GroupsService, GroupsDao, Logger], + providers: [GroupsService, GroupsDao, Logger, PeopleService, PeopleDao], }) export class GroupsModule {} diff --git a/src/groups/groups.service.ts b/src/groups/groups.service.ts index bf52da3ff59f23a994c483eba9e9746b79f5d406..59b939eb984677467a322216b9e21a2182a053c9 100644 --- a/src/groups/groups.service.ts +++ b/src/groups/groups.service.ts @@ -27,6 +27,14 @@ export class GroupsService { ): Promise<GroupEntity | void> => this._groupsDao.findByIdAndUpdateRole(id, role, personId, action); + updateManyByRole = ( + id: string, + role: string, + peopleIds: string[], + action: string, + ): Promise<GroupEntity | void> => + this._groupsDao.findByIdAndUpdateManyByRole(id, role, peopleIds, action); + delete(id: string): Promise<GroupEntity | void> { return this._groupsDao.findById(id).then((res) => { if (res) { diff --git a/src/internships/dao/internships.dao.ts b/src/internships/dao/internships.dao.ts index 22a08178f9e288cc68691af8cf6e3a570ea36dfa..c42d59e14a839d0cd7656b8173c7334fea99a1ae 100644 --- a/src/internships/dao/internships.dao.ts +++ b/src/internships/dao/internships.dao.ts @@ -120,9 +120,7 @@ export class InternshipDao { content: string | boolean, ): Promise<Internship | void> => new Promise((resolve, reject) => { - console.log('%s/%s: {%s}', studentId, state, content); if (!isStateValid(state)) reject(BAD_REQUEST); - console.log(content); let nextState: string, contentHolder: string; let valid = false; switch (state) { diff --git a/src/people/dao/people.dao.ts b/src/people/dao/people.dao.ts index 33fd035045f70b36ddbd24d83c3c6093fc6db34c..bb03a1deee6989170a1cfd947e8a83900ca97cec 100644 --- a/src/people/dao/people.dao.ts +++ b/src/people/dao/people.dao.ts @@ -12,6 +12,8 @@ import { People } from '../schemas/people.schema'; import * as Mailgun from 'mailgun-js'; import config from 'src/config'; import * as bcrypt from 'bcrypt'; +import { PeopleEntity } from '../entities/people.entity'; +import { CONFLICT } from 'src/shared/HttpError'; @Injectable() export class PeopleDao { @@ -73,6 +75,41 @@ export class PeopleDao { }); }; + saveMany = async (people: CreatePeopleDto[]): Promise<PeopleEntity[]> => + Promise.all( + people.map( + (person): Promise<PeopleEntity> => + new Promise((resolve, reject) => { + this._peopleModel.findOne( + { email: person.email }, + {}, + {}, + (err, value) => { + if (err) { + reject(err.message); + } else if (!value) { + // Person does not exist -> create password + add to database + this.secret().then((value) => { + person.passwordHash = value; + new this._peopleModel(person).save((err, value) => { + if (err) reject(err.message); + if (!value) reject(new InternalServerErrorException()); + this.sendPassword(person.email, person.passwordHash); + resolve(value); + }); + }); + } else { + resolve(value); + } + }, + ); + }), + ), + ); + // Check for existing people in database based on email + // Add missing people + // Get all added people as PeopleEntity to have access to IDs + findByIdAndUpdate = ( id: string, people: UpdatePeopleDto, diff --git a/src/people/dto/create-people.dto.ts b/src/people/dto/create-people.dto.ts index 2dc4098deb5928378ec1f70e3d3641721fe9a49f..5906a213fb87b974f7d534e4ad99f33547691fed 100644 --- a/src/people/dto/create-people.dto.ts +++ b/src/people/dto/create-people.dto.ts @@ -1,27 +1,26 @@ -import { IsBoolean, IsString, IsNotEmpty, IsOptional, IsNumber } from 'class-validator'; - -export class CreatePeopleDto { - - @IsNumber() - @IsNotEmpty() - ine: number; - - @IsString() - @IsNotEmpty() - firstname: string; - - @IsString() - @IsNotEmpty() - lastname: string; - - @IsOptional() - passwordHash: string; - - @IsString() - @IsNotEmpty() - email: string; - - @IsNotEmpty() - role: number; - -} +import { IsBoolean, IsString, IsNotEmpty, IsOptional, IsNumber } from 'class-validator'; + +export class CreatePeopleDto { + + @IsNumber() + @IsNotEmpty() + ine: number; + + @IsString() + @IsNotEmpty() + firstname: string; + + @IsString() + @IsNotEmpty() + lastname: string; + + @IsOptional() + passwordHash: string; + + @IsString() + @IsNotEmpty() + email: string; + + @IsOptional() + role: number; +} diff --git a/src/people/entities/people.entity.ts b/src/people/entities/people.entity.ts index 5a2829e199c5e228fa613f4eff243081a18f37f0..273de2ed64bed9b116d6640c0a609323d5353cf5 100644 --- a/src/people/entities/people.entity.ts +++ b/src/people/entities/people.entity.ts @@ -1,15 +1,15 @@ -import { People } from '../schemas/people.schema'; - -export class PeopleEntity { - _id: string; - ine: number; - firstname: string; - lastname: string; - email: string; - passwordHash: string; - role: number; - - constructor(partial: Partial<People>) { - Object.assign(this, partial); - } -} +import { People } from '../schemas/people.schema'; + +export class PeopleEntity { + _id: string; + ine: number; + firstname: string; + lastname: string; + email: string; + passwordHash: string; + role: number; + + constructor(partial: Partial<People>) { + Object.assign(this, partial); + } +} diff --git a/src/people/people.controller.ts b/src/people/people.controller.ts index 0837bc2eb3c625217a86ae0585cfe4440bf604d3..68b8d7593b343c8f2564f213ad40813cdd7dea4f 100644 --- a/src/people/people.controller.ts +++ b/src/people/people.controller.ts @@ -29,7 +29,7 @@ export class PeopleController { constructor(private readonly _peopleService: PeopleService) {} - @UseGuards(AuthGuard('jwt')) + //@UseGuards(AuthGuard('jwt')) @Get() findAll(): Promise<PeopleEntity[] | void> { return this._peopleService.findAll(); diff --git a/src/people/people.service.ts b/src/people/people.service.ts index 2c3581978917806ea11ee85fbe918404cc332b59..10d3195ca127fa10d37d49b6ce49730d620617c6 100644 --- a/src/people/people.service.ts +++ b/src/people/people.service.ts @@ -24,6 +24,10 @@ export class PeopleService { return this._peopleDao.save(people); } + createMany = (people: CreatePeopleDto[]): Promise<PeopleEntity[]> => { + return this._peopleDao.saveMany(people); + }; + update = ( id: string, people: UpdatePeopleDto, diff --git a/src/shared/Roles.ts b/src/shared/Roles.ts index 317633e05f41a3ba31dee1a4283e965842af92e7..48fa28a41351e96ecfba88101b84c87f7c52a729 100644 --- a/src/shared/Roles.ts +++ b/src/shared/Roles.ts @@ -12,3 +12,18 @@ export const ROLES = [ export const isRoleValid = (potentialRole: string): boolean => ROLES.includes(potentialRole); + +export const roleValue = (role: string): number => { + switch (role) { + case ROLE_STUDENT: + return 0; + case ROLE_SECRETARY: + return 1; + case ROLE_RESPONSIBLE: + return 2; + case ROLE_ADMIN: + return -9999; + default: + return -1; + } +};