Skip to content
Snippets Groups Projects
Commit 6c0d3f65 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

Merge branch 'feat/endpoint-structures' into 'dev'

feat: add new endpoint for structures (jointure on modules)

See merge request web-et-numerique/pamn_plateforme-des-acteurs-de-la-mediation-numerique/pamn_server!64
parents 736bb96f 509f30f9
No related branches found
No related tags found
3 merge requests!96release V1.10.0,!73Dev,!64feat: add new endpoint for structures (jointure on modules)
import { HttpException, HttpModule, HttpStatus } from '@nestjs/common'; import { HttpModule } from '@nestjs/common';
import { getModelToken } from '@nestjs/mongoose'; import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ConfigurationModule } from '../configuration/configuration.module'; import { ConfigurationModule } from '../configuration/configuration.module';
...@@ -47,21 +47,35 @@ describe('AdminController', () => { ...@@ -47,21 +47,35 @@ describe('AdminController', () => {
}); });
it('should get pending attachments', async () => { it('should get pending attachments', async () => {
const result = [{name: "MJC Route de vienne", address: "14 chemin des platanes"}, {name: "Mairie Lyon 7eme", address: "21 boulevard martin"}]; const result = [
{ name: 'MJC Route de vienne', address: '14 chemin des platanes' },
{ name: 'Mairie Lyon 7eme', address: '21 boulevard martin' },
];
jest.spyOn(controller, 'getPendingAttachments').mockImplementation(async (): Promise<any> => result); jest.spyOn(controller, 'getPendingAttachments').mockImplementation(async (): Promise<any> => result);
expect(await controller.getPendingAttachments()).toBe(result); expect(await controller.getPendingAttachments()).toBe(result);
}); });
it('should validate pending structure', async () => { it('should validate pending structure', async () => {
const result = [{name: "MJC Route de vienne", address: "14 chemin des platanes"}]; const result = [{ name: 'MJC Route de vienne', address: '14 chemin des platanes' }];
const structure: PendingStructureDto = {userEmail:"martin@mjc.fr", structureId: "1", structureName:"MJC Route de vienne"}; const structure: PendingStructureDto = {
userEmail: 'martin@mjc.fr',
structureId: '1',
structureName: 'MJC Route de vienne',
};
jest.spyOn(controller, 'validatePendingStructure').mockImplementation(async (): Promise<any> => result); jest.spyOn(controller, 'validatePendingStructure').mockImplementation(async (): Promise<any> => result);
expect(await controller.validatePendingStructure(structure)).toBe(result); expect(await controller.validatePendingStructure(structure)).toBe(result);
}); });
it('should refuse pending structure', async () => { it('should refuse pending structure', async () => {
const result = [{name: "MJC Route de vienne", address: "14 chemin des platanes"}, {name: "Mairie Lyon 7eme", address: "21 boulevard martin"}]; const result = [
const structure: PendingStructureDto = {userEmail:"martin@mjc.fr", structureId: "1", structureName:"MJC Route de vienne"}; { name: 'MJC Route de vienne', address: '14 chemin des platanes' },
{ name: 'Mairie Lyon 7eme', address: '21 boulevard martin' },
];
const structure: PendingStructureDto = {
userEmail: 'martin@mjc.fr',
structureId: '1',
structureName: 'MJC Route de vienne',
};
jest.spyOn(controller, 'refusePendingStructure').mockImplementation(async (): Promise<any> => result); jest.spyOn(controller, 'refusePendingStructure').mockImplementation(async (): Promise<any> => result);
expect(await controller.refusePendingStructure(structure)).toBe(result); expect(await controller.refusePendingStructure(structure)).toBe(result);
}); });
......
...@@ -29,7 +29,7 @@ import { NewsletterModule } from './newsletter/newsletter.module'; ...@@ -29,7 +29,7 @@ import { NewsletterModule } from './newsletter/newsletter.module';
AdminModule, AdminModule,
PostsModule, PostsModule,
TempUserModule, TempUserModule,
NewsletterModule NewsletterModule,
], ],
controllers: [AppController], controllers: [AppController],
}) })
......
...@@ -19,7 +19,10 @@ import { CategoriesAccompagnement, CategoriesAccompagnementSchema } from './sche ...@@ -19,7 +19,10 @@ import { CategoriesAccompagnement, CategoriesAccompagnementSchema } from './sche
]), ]),
], ],
controllers: [CategoriesFormationsController, CategoriesAccompagnementController, CategoriesOthersController], controllers: [CategoriesFormationsController, CategoriesAccompagnementController, CategoriesOthersController],
exports: [CategoriesFormationsService], exports: [CategoriesFormationsService, CategoriesAccompagnementService, CategoriesOthersService],
providers: [CategoriesFormationsService, CategoriesAccompagnementService, CategoriesOthersService], providers: [CategoriesFormationsService, CategoriesAccompagnementService, CategoriesOthersService],
}) })
export class CategoriesModule {} export class CategoriesModule {
id: string;
text: any;
}
import { Db } from 'mongodb';
import { getDb } from '../migrations-utils/db';
import * as fs from 'fs';
export const up = async () => {
const db: Db = await getDb();
const data = fs.readFileSync('/app/src/migrations/data/newsletter-data.json', 'utf8');
const parsedData = JSON.parse(data);
db.collection('newslettersubscriptions').insertMany(parsedData);
};
export const down = async () => {
const db: Db = await getDb();
/*
Code you downgrade script here!
*/
};
...@@ -14,6 +14,11 @@ import { DateTime } from 'luxon'; ...@@ -14,6 +14,11 @@ import { DateTime } from 'luxon';
import { IUser } from '../../users/interfaces/user.interface'; import { IUser } from '../../users/interfaces/user.interface';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { OwnerDto } from '../../users/dto/owner.dto'; import { OwnerDto } from '../../users/dto/owner.dto';
import { CategoriesFormationsModule } from '../../categories/schemas/categoriesFormationsModule.schema';
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';
@Injectable() @Injectable()
export class StructuresService { export class StructuresService {
...@@ -106,6 +111,99 @@ export class StructuresService { ...@@ -106,6 +111,99 @@ export class StructuresService {
return this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec(); return this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec();
} }
public async findAllFormated(
formationCategories: CategoriesFormations[],
accompagnementCategories: CategoriesAccompagnement[],
otherCategories: CategoriesOthers[]
): Promise<StructureDocument[]> {
const structures = await this.structureModel.find({ deletedAt: { $exists: false } }).exec();
// Update structures coord and address before sending them
await Promise.all(
structures.map((structure: StructureDocument) => {
// If structre has no address, add it
if (!structure.address || structure.coord.length <= 0) {
return this.getStructurePosition(structure).then((postition: StructureDocument) => {
this.structureModel
.findByIdAndUpdate(Types.ObjectId(structure._id), {
address: postition.address,
coord: postition.coord,
})
.exec();
});
}
})
);
return (await this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec()).map(
(structure) => {
structure.proceduresAccompaniment = this.mapModules(
structure.proceduresAccompaniment,
accompagnementCategories.find((category) => category.name === 'Accompagnement des démarches').modules
);
structure.labelsQualifications = this.mapModules(
structure.labelsQualifications,
otherCategories.find((category) => category.name === 'Labels et qualifications').modules
);
structure.publics = this.mapModules(
structure.publics,
otherCategories.find((category) => category.name === 'Publics acceptés').modules
);
structure.accessModality = this.mapModules(
structure.accessModality,
otherCategories.find((category) => category.name === "Modalités d'accès").modules
);
structure.publicsAccompaniment = this.mapModules(
structure.publicsAccompaniment,
otherCategories.find((category) => category.name === 'Accompagnement des publics spécifique').modules
);
structure.equipmentsAndServices = this.mapModules(
structure.equipmentsAndServices,
otherCategories.find((category) => category.name === 'Équipements et services proposés').modules
);
structure.baseSkills = this.mapFormationModules(
structure.baseSkills,
formationCategories.find((category) => category.name === 'Les compétences de base').modules
);
structure.accessRight = this.mapFormationModules(
structure.accessRight,
formationCategories.find((category) => category.name === 'Accès aux droits').modules
);
structure.socialAndProfessional = this.mapFormationModules(
structure.socialAndProfessional,
formationCategories.find((category) => category.name === 'Insertion sociale et professionnelle').modules
);
structure.parentingHelp = this.mapFormationModules(
structure.parentingHelp,
formationCategories.find((category) => category.name === 'Aide à la parentalité').modules
);
structure.digitalCultureSecurity = this.mapFormationModules(
structure.digitalCultureSecurity,
formationCategories.find((category) => category.name === 'Culture et sécurité numérique').modules
);
return structure;
}
);
}
public mapFormationModules(structureModule: string[], baseModule: CategoriesFormationsModule[]): string[] {
if (structureModule == []) {
return [];
}
return structureModule.map((id) => {
const formatedText = baseModule.find((module) => module.display_id === id)?.text;
return formatedText ? formatedText : id;
});
}
public mapModules(structureModule: string[], baseModule: CategoriesModule[]): string[] {
if (structureModule == []) {
return [];
}
return structureModule.map((id) => {
const formatedText = baseModule.find((module) => module.id === id)?.text;
return formatedText ? formatedText : id;
});
}
public async update(idStructure: string, structure: structureDto): Promise<Structure> { public async update(idStructure: string, structure: structureDto): Promise<Structure> {
const oldStructure = await this.findOne(idStructure); const oldStructure = await this.findOne(idStructure);
if (!_.isEqual(oldStructure.address, structure.address)) { if (!_.isEqual(oldStructure.address, structure.address)) {
......
...@@ -15,6 +15,9 @@ import { ...@@ -15,6 +15,9 @@ import {
import { ApiParam } from '@nestjs/swagger'; import { ApiParam } from '@nestjs/swagger';
import { Types } from 'mongoose'; import { Types } from 'mongoose';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CategoriesAccompagnementService } from '../categories/services/categories-accompagnement.service';
import { CategoriesFormationsService } from '../categories/services/categories-formations.service';
import { CategoriesOthersService } from '../categories/services/categories-others.service';
import { CreateTempUserDto } from '../temp-user/dto/create-temp-user.dto'; import { CreateTempUserDto } from '../temp-user/dto/create-temp-user.dto';
import { TempUserService } from '../temp-user/temp-user.service'; import { TempUserService } from '../temp-user/temp-user.service';
import { Roles } from '../users/decorators/roles.decorator'; import { Roles } from '../users/decorators/roles.decorator';
...@@ -33,7 +36,10 @@ export class StructuresController { ...@@ -33,7 +36,10 @@ export class StructuresController {
private readonly httpService: HttpService, private readonly httpService: HttpService,
private readonly structureService: StructuresService, private readonly structureService: StructuresService,
private readonly userService: UsersService, private readonly userService: UsersService,
private readonly tempUserService: TempUserService private readonly tempUserService: TempUserService,
private readonly categoriesFormationsService: CategoriesFormationsService,
private readonly categoriesOthersService: CategoriesOthersService,
private readonly categoriesAccompagnementService: CategoriesAccompagnementService
) {} ) {}
/** /**
...@@ -79,6 +85,14 @@ export class StructuresController { ...@@ -79,6 +85,14 @@ export class StructuresController {
return this.structureService.findAll(); return this.structureService.findAll();
} }
@Get('formated')
public async findAllFormated(): Promise<Structure[]> {
const formationCategories = await this.categoriesFormationsService.findAll();
const accompagnementCategories = await this.categoriesAccompagnementService.findAll();
const otherCategories = await this.categoriesOthersService.findAll();
return this.structureService.findAllFormated(formationCategories, accompagnementCategories, otherCategories);
}
@Post(':id/isClaimed') @Post(':id/isClaimed')
public async isClaimed(@Param('id') id: string, @Body() user?: User): Promise<boolean> { public async isClaimed(@Param('id') id: string, @Body() user?: User): Promise<boolean> {
return this.structureService.isClaimed(id, user); return this.structureService.isClaimed(id, user);
......
import { HttpModule } from '@nestjs/common'; import { HttpModule } from '@nestjs/common';
import { getModelToken } from '@nestjs/mongoose'; import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { CategoriesModule } from '../categories/categories.module';
import { ConfigurationModule } from '../configuration/configuration.module'; import { ConfigurationModule } from '../configuration/configuration.module';
import { MailerService } from '../mailer/mailer.service'; import { MailerService } from '../mailer/mailer.service';
import { Structure } from '../structures/schemas/structure.schema'; import { Structure } from '../structures/schemas/structure.schema';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment