diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index 999c6d949ba0026bdc654d172dc703459030ed50..f35cae88243539fc12daa9aa0adf6c9f47499cb3 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -1,14 +1,14 @@ -import { Body, Delete, Param } from '@nestjs/common'; -import { Controller, Get, Post, UseGuards } from '@nestjs/common'; +import { Body, Delete, Param, Controller, Get, Post, UseGuards } from '@nestjs/common'; import { ApiOperation, ApiParam } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; -import { NewsletterSubscriptionDocument } from '../newsletter/newsletter-subscription.schema'; +import { NewsletterSubscription } from '../newsletter/newsletter-subscription.schema'; import { NewsletterService } from '../newsletter/newsletter.service'; import { StructuresService } from '../structures/services/structures.service'; import { Roles } from '../users/decorators/roles.decorator'; import { RolesGuard } from '../users/guards/roles.guard'; import { UsersService } from '../users/users.service'; import { PendingStructureDto } from './dto/pending-structure.dto'; +import { UnclaimedStructureDto } from './dto/unclaimed-structure-dto'; @Controller('admin') export class AdminController { @@ -24,7 +24,7 @@ export class AdminController { @ApiOperation({ description: 'Get pending structre for validation' }) public async getPendingAttachments(): Promise<PendingStructureDto[]> { const pendingStructure = await this.usersService.getPendingStructures(); - return await Promise.all( + return Promise.all( pendingStructure.map(async (structure) => { structure.structureName = (await this.structuresService.findOne(structure.structureId)).structureName; return structure; @@ -32,6 +32,31 @@ export class AdminController { ); } + @UseGuards(JwtAuthGuard, RolesGuard) + @Roles('admin') + @Get('adminStructuresList') + @ApiOperation({ description: 'Get pending structre for validation' }) + public async getAdminStructuresList(): Promise<any> { + const structuresList = { claimed: [], inClaim: [], toClaim: [] }; + structuresList.inClaim = await this.getPendingAttachments(); + structuresList.toClaim = (await this.structuresService.findAllUnclaimed()).filter( + (demand) => !structuresList.inClaim.find((elem) => elem.structureId == demand.structureId) + ); + structuresList.claimed = (await this.structuresService.findAll()) + .filter( + (demand) => + !structuresList.inClaim.find((elem) => elem.structureId == demand.id) && + !structuresList.toClaim.find((elem) => elem.structureId == demand.id) + ) + .map((structure) => { + return { structureId: structure.id, structureName: structure.structureName }; + }); + structuresList.claimed.sort((a, b) => a.structureName.localeCompare(b.structureName)); + structuresList.inClaim.sort((a, b) => a.structureName.localeCompare(b.structureName)); + structuresList.toClaim.sort((a, b) => a.structureName.localeCompare(b.structureName)); + return structuresList; + } + @UseGuards(JwtAuthGuard, RolesGuard) @Roles('admin') @Post('validatePendingStructure') @@ -45,7 +70,7 @@ export class AdminController { true ); const pendingStructure = await this.usersService.getPendingStructures(); - return await Promise.all( + return Promise.all( pendingStructure.map(async (pendingStructureData) => { pendingStructureData.structureName = ( await this.structuresService.findOne(pendingStructureData.structureId) @@ -68,7 +93,7 @@ export class AdminController { false ); const pendingStructure = await this.usersService.getPendingStructures(); - return await Promise.all( + return Promise.all( pendingStructure.map(async (pendingStructureData) => { pendingStructureData.structureName = ( await this.structuresService.findOne(pendingStructureData.structureId) @@ -123,7 +148,7 @@ export class AdminController { @Roles('admin') @Delete('newsletterSubscription/:email') @ApiParam({ name: 'email', type: String, required: true }) - public async unsubscribeUserFromNewsletter(@Param() params) { - return await this.newsletterService.deleteOneEmail(params.email); + public async unsubscribeUserFromNewsletter(@Param() params): Promise<NewsletterSubscription> { + return this.newsletterService.deleteOneEmail(params.email); } } diff --git a/src/admin/dto/unclaimed-structure-dto.ts b/src/admin/dto/unclaimed-structure-dto.ts new file mode 100644 index 0000000000000000000000000000000000000000..c57eedbd30d908debab1d539676731e1a7c79388 --- /dev/null +++ b/src/admin/dto/unclaimed-structure-dto.ts @@ -0,0 +1,14 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class UnclaimedStructureDto { + @IsNotEmpty() + @IsString() + @ApiProperty({ type: String }) + readonly structureId: string; + + @IsNotEmpty() + @IsString() + @ApiProperty({ type: String }) + structureName: string; +} diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 8e582acc9da8d8d3e96fbd098998d4ea397a83b4..6b70fb759afb91ac0cdbf13aa65569eb43dfc2a1 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -20,6 +20,7 @@ import { CategoriesModule } from '../../categories/categories.module'; import { CategoriesAccompagnement } from '../../categories/schemas/categoriesAccompagnement.schema'; import { CategoriesFormations } from '../../categories/schemas/categoriesFormations.schema'; import { CategoriesOthers } from '../../categories/schemas/categoriesOthers.schema'; +import { UnclaimedStructureDto } from '../../admin/dto/unclaimed-structure-dto'; @Injectable() export class StructuresService { @@ -152,6 +153,19 @@ export class StructuresService { return this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec(); } + public async findAllUnclaimed(): Promise<UnclaimedStructureDto[]> { + const structures = await this.structureModel.find({ deletedAt: { $exists: false } }).exec(); + const unclaimedStructures = []; + await Promise.all( + structures.map(async (structure: StructureDocument) => { + if (!(await this.userService.isStructureClaimed(structure.id))) { + unclaimedStructures.push({ structureId: structure.id, structureName: structure.structureName }); + } + }) + ); + return unclaimedStructures; + } + public async populateES(): Promise<StructureDocument[]> { const structures = await this.structureModel.find({ deletedAt: { $exists: false } }).exec(); await Promise.all(