Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • web-et-numerique/factory/pamn_plateforme-des-acteurs-de-la-mediation-numerique/pamn_server
1 result
Show changes
Showing
with 100 additions and 96 deletions
import { Db } from 'mongodb';
import { getDb } from '../migrations-utils/db';
export const up = async () => {
const db: Db = await getDb();
await db
.collection('categories')
.updateOne({ id: 'onlineProcedures' }, { $pull: { modules: { id: 'disability', name: 'Handicap' } } });
console.log('Updated : "Handicap" removed to "onlineProcedures" document in "categories" collection');
};
export const down = async () => {
const db: Db = await getDb();
await db
.collection('categories')
.updateOne({ id: 'onlineProcedures' }, { $push: { modules: { id: 'disability', name: 'Handicap' } } });
console.log('Downgraded : "Handicap" added from "onlineProcedures" document in "categories" collection');
};
import { HttpModule } from '@nestjs/axios';
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { ConfigurationModule } from '../configuration/configuration.module';
import { NewsletterSubscription } from './newsletter-subscription.schema';
import { NewsletterController } from './newsletter.controller';
......@@ -14,7 +14,7 @@ describe('NewsletterController', () => {
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [ConfigurationModule, HttpModule],
providers: [
NewsletterService,
......
import { HttpModule } from '@nestjs/axios';
import { HttpStatus } from '@nestjs/common';
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { INewsletterSubscription } from './interface/newsletter-subscription.interface';
import { NewsletterSubscription } from './newsletter-subscription.schema';
import { NewsletterService } from './newsletter.service';
......@@ -28,7 +28,7 @@ describe('NewsletterService', () => {
beforeEach(async () => {
jest.resetModules(); // Most important - it clears the cache
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [HttpModule],
providers: [
NewsletterService,
......@@ -133,7 +133,6 @@ describe('NewsletterService', () => {
mockNewsletterModel.exec.mockResolvedValueOnce(undefined);
try {
await newsletterService.newsletterUnsubscribe('test@test.com');
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toEqual('Email not found');
......
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { onlineMediationMockData, singleOnlineMediationMock } from '../../test/mock/data/onlineMediation.mock.data';
import { OnlineMediationController } from './onlineMediation.controller';
import { OnlineMediation } from './onlineMediation.schema';
......@@ -14,7 +14,7 @@ describe('UserRegistryController', () => {
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [],
providers: [
{
......
import { HttpModule } from '@nestjs/axios';
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import * as ejs from 'ejs';
import { onlineMediationMockData } from '../../test/mock/data/onlineMediation.mock.data';
import { ConfigurationService } from '../configuration/configuration.service';
......@@ -39,7 +39,7 @@ describe('userRegistryService', () => {
const mailerSpy = jest.spyOn(mockMailService, 'send');
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [MailerModule, HttpModule],
providers: [
OnlineMediationService,
......
import { HttpModule, HttpService } from '@nestjs/axios';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { AxiosResponse } from 'axios';
import { of } from 'rxjs';
import { ConfigurationModule } from '../configuration/configuration.module';
......@@ -12,7 +12,7 @@ describe('PagesController', () => {
get: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [ConfigurationModule, HttpModule],
providers: [
{
......
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import {
createPersonalOffersDtoDataMock,
personalOffersDataMock,
......@@ -20,7 +20,7 @@ describe('PersonalOffersController', () => {
let personalOffersController: PersonalOffersController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
controllers: [PersonalOffersController],
providers: [
{
......@@ -57,7 +57,6 @@ describe('PersonalOffersController', () => {
it('should get personal offer does not exist', async () => {
try {
await personalOffersController.find('abcd');
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Personal offer does not exist');
......@@ -71,7 +70,6 @@ describe('PersonalOffersController', () => {
const req = { user: { _id: '6036721022462b001334c4bb' } };
try {
await personalOffersController.create(req, createPersonalOffersDtoDataMock[3]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Empty personal offer');
......@@ -88,7 +86,6 @@ describe('PersonalOffersController', () => {
const req = { user: { _id: '6036721022462b001334c4bb' } };
try {
await personalOffersController.create(req, createPersonalOffersDtoDataMock[1]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Personal offer already exist in the structure');
......@@ -99,7 +96,6 @@ describe('PersonalOffersController', () => {
const req = { user: { _id: '6036721022462b001334c4bb' } };
try {
await personalOffersController.create(req, createPersonalOffersDtoDataMock[2]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Structure not found for the personal offer attachment');
......@@ -110,7 +106,6 @@ describe('PersonalOffersController', () => {
const req = { user: { _id: 'unIdQuiExistePasTropTrop' } };
try {
await personalOffersController.create(req, createPersonalOffersDtoDataMock[0]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('User not found for the personal offer attachment');
......@@ -128,7 +123,6 @@ describe('PersonalOffersController', () => {
it('should get invalid personal offer id', async () => {
try {
await personalOffersController.update('abcd', updatePersonalOffersDtoDataMock[1]);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Invalid personal offer id for update');
......@@ -144,7 +138,6 @@ describe('PersonalOffersController', () => {
it('should get invalid personal offer id', async () => {
try {
await personalOffersController.delete('abcd');
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Invalid personal offer id for deletion');
......
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import {
createPersonalOffersDtoDataMock,
personalOffersDataMock,
......@@ -23,7 +23,7 @@ describe('PersonalOffersService', () => {
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
providers: [
PersonalOffersService,
{
......@@ -61,14 +61,13 @@ describe('PersonalOffersService', () => {
it('should return exception if personal offer is not found for given id', async () => {
personalOfferModelMock.findById.mockReturnThis();
personalOfferModelMock.exec.mockResolvedValueOnce(null);
let error;
try {
await personalOffersService.findOne('abcd');
expect(true).toBe(false);
} catch (e) {
error = e;
expect(e.message).toBe('Personal offer does not exist');
expect(e.status).toBe(404);
}
expect(error.message).toBe('Personal offer does not exist');
expect(error.status).toBe(404);
});
});
......@@ -90,14 +89,13 @@ describe('PersonalOffersService', () => {
it('should return exception if personal offer is not found for given id', async () => {
personalOfferModelMock.findById.mockReturnThis();
personalOfferModelMock.exec.mockResolvedValueOnce(null);
let error;
try {
await personalOffersService.update('abcd', updatePersonalOffersDtoDataMock[1]);
expect(true).toBe(false);
} catch (e) {
error = e;
expect(e.message).toBe('Invalid personal offer id for update');
expect(e.status).toBe(400);
}
expect(error.message).toBe('Invalid personal offer id for update');
expect(error.status).toBe(400);
});
});
......@@ -110,14 +108,13 @@ describe('PersonalOffersService', () => {
it('should return exception if personal offer is not found for given id', async () => {
personalOfferModelMock.findById.mockReturnThis();
personalOfferModelMock.exec.mockResolvedValueOnce(null);
let error;
try {
await personalOffersService.delete('abcd');
expect(true).toBe(false);
} catch (e) {
error = e;
expect(e.message).toBe('Invalid personal offer id for deletion');
expect(e.status).toBe(400);
}
expect(error.message).toBe('Invalid personal offer id for deletion');
expect(error.status).toBe(400);
});
});
});
import { HttpModule, HttpService } from '@nestjs/axios';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { AxiosResponse } from 'axios';
import { of } from 'rxjs';
import { ConfigurationModule } from '../configuration/configuration.module';
......@@ -21,7 +21,7 @@ describe('PostsController', () => {
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [ConfigurationModule, HttpModule],
providers: [
{
......
import { HttpModule } from '@nestjs/axios';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import * as _ from 'lodash';
import { ConfigurationModule } from '../configuration/configuration.module';
import { TagEnum } from './enums/tag.enum';
......@@ -207,7 +207,7 @@ describe('PostsService', () => {
const tagsData = [...locationtags, ...publictags, ...otherTags];
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [ConfigurationModule, HttpModule],
providers: [PostsService],
}).compile();
......
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { Model, Query } from 'mongoose';
import { mockFormattedStructures } from '../../../test/mock/services/structures-export.mock.service';
import { StructureFormatted } from '../interfaces/structure-formatted.interface';
......@@ -143,7 +143,7 @@ describe('StructuresExportService', () => {
let structureModel: Model<StructureDocument>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
providers: [
StructuresExportService,
{
......
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { of } from 'rxjs';
import { DataGouvStructure } from '../interfaces/data-gouv-structure.interface';
import { HttpService } from '@nestjs/axios';
......@@ -7,21 +7,20 @@ import { AxiosResponse } from 'axios';
import { mockGouvStructure, mockGouvStructureToResinFormat } from '../../../test/mock/data/gouvStructures.mock.data';
import { StructuresSearchService } from './structures-search.service';
import { StructuresService } from './structures.service';
import { MailerService } from '../../mailer/mailer.service';
import { StructuresImportService } from './structures-import.service';
import { MailerService } from '../../mailer/mailer.service';
describe('StructuresImportService', () => {
let structuresImportService: StructuresImportService;
let structuresSearchService: StructuresSearchService;
let structuresService: StructuresService;
let mailerService: MailerService;
const httpServiceMock = {
get: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
providers: [
StructuresImportService,
{
......@@ -71,7 +70,6 @@ describe('StructuresImportService', () => {
structuresImportService = module.get<StructuresImportService>(StructuresImportService);
structuresSearchService = module.get<StructuresSearchService>(StructuresSearchService);
structuresService = module.get<StructuresService>(StructuresService);
mailerService = module.get<MailerService>(MailerService);
});
describe('importDataGouvStructures', () => {
......
......@@ -4,7 +4,6 @@ import { HttpService } from '@nestjs/axios';
import { Model, Types } from 'mongoose';
import { Structure, StructureDocument } from '../schemas/structure.schema';
import { InjectModel } from '@nestjs/mongoose';
import { Cron, CronExpression } from '@nestjs/schedule';
import { lastValueFrom } from 'rxjs';
import * as config from './structures-export-configs/services-config.json';
import { Week } from '../../shared/schemas/week.schema';
......
import { Logger } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { StructuresForSearchServiceMock } from '../../../test/mock/services/structures-for-search.mock.service';
import { MailerModule } from '../../mailer/mailer.module';
import { SearchModule } from '../../search/search.module';
......@@ -11,7 +11,7 @@ describe('StructuresSearchService', () => {
let structureSearchService: StructuresSearchService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [MailerModule, SearchModule, ConfigModule],
providers: [
StructuresSearchService,
......
......@@ -2,7 +2,7 @@ import { HttpModule, HttpService } from '@nestjs/axios';
import { HttpStatus } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import * as bcrypt from 'bcrypt';
import { Types } from 'mongoose';
import { of } from 'rxjs';
......@@ -225,7 +225,7 @@ describe('StructuresService', () => {
let structureService: StructuresService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [HttpModule, MailerModule, SearchModule, ConfigModule],
providers: [
StructuresService,
......@@ -522,7 +522,6 @@ describe('StructuresService', () => {
jest.spyOn(structureService, 'findOne').mockResolvedValue(null);
try {
await structureService.addPersonalOffer('abcd', personalOfferDocumentMock);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Structure not found for the personal offer attachment');
......@@ -533,7 +532,6 @@ describe('StructuresService', () => {
jest.spyOn(structureService, 'findOne').mockResolvedValue(structuresDocumentDataMock[1]);
try {
await structureService.addPersonalOffer('6093ba0e2ab5775cfc01ed3e', personalOfferDocumentMock);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Personal offer already exist in the structure');
......
......@@ -610,39 +610,7 @@ export class StructuresService {
}
if (structure.address.postcode) {
let inseeCode: string = null;
const req = `https://apicarto.ign.fr/api/codes-postaux/communes/${structure.address.postcode}`;
this.logger.debug(`Request : ${req}`);
let res;
try {
res = await lastValueFrom(this.httpService.get(encodeURI(req)));
} catch (e) {
this.logger.error(`Request ${encodeURI(req)} failed: ${e}`);
return structure;
}
if (res.data?.length) {
if (res.data.length == 1) {
this.logger.debug(`Insee code ${res.data[0].codeCommune} for ${structure.address.commune}`);
inseeCode = res.data[0].codeCommune;
} else {
// if several commune for this postcode, find which commune we are looking for the insee code
const communes = res.data.filter((commune) => commune.nomCommune == structure.address.commune);
if (communes.length) {
this.logger.debug(`Insee code ${communes[0].codeCommune} for ${structure.address.commune}`);
inseeCode = communes[0].codeCommune;
} else {
this.logger.error(
`Insee code of commune ${structure.address.commune} not found for postcode ${
structure.address.postcode
} in ${JSON.stringify(res.data)} (${req}) --> Postcode reset for future execution`
);
// Reset postcode which is probably wrong because of previous BAL city matching error (cf. issue pamn_client#403 )
structure.address.postcode = '';
}
}
}
const inseeCode = await this.getInseeCode(structure.address.postcode, structure.address.commune);
if (inseeCode) {
structure.address.inseeCode = inseeCode;
const ctm: Module = await this.categoriesService.findCtmByInseeCode(inseeCode);
......@@ -655,6 +623,42 @@ export class StructuresService {
return structure;
}
public async getInseeCode(postcode: string, municipality: string): Promise<string | null> {
const req = `https://apicarto.ign.fr/api/codes-postaux/communes/${postcode}`;
this.logger.debug(`Request : ${req}`);
let res;
try {
res = await lastValueFrom(this.httpService.get(encodeURI(req)));
} catch (e) {
this.logger.error(`Request ${encodeURI(req)} failed: ${e}`);
return null;
}
if (res.data?.length) {
if (res.data.length == 1) {
this.logger.debug(`Insee code ${res.data[0].codeCommune} for ${municipality}`);
return res.data[0].codeCommune;
} else {
// if several municipality for this postcode, find for which municipality we are looking for the insee code
const communes = res.data.filter((commune) => commune.nomCommune == municipality);
if (communes.length) {
this.logger.debug(`Insee code ${communes[0].codeCommune} for ${municipality}`);
return communes[0].codeCommune;
} else {
this.logger.error(
`Insee code of commune ${municipality} not found for postcode ${postcode} in ${JSON.stringify(
res.data
)} (${req})`
);
return null;
}
}
} else {
return null;
}
}
/**
* Get city formatted without "Arrondissement"
* @param {searchQuery} data - Query address
......
import { HttpService } from '@nestjs/axios';
import { HttpStatus } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { of } from 'rxjs';
import { PhotonResponseMock } from '../../test/mock/data/dataPhoton.mock.data';
import { structureDtoMock } from '../../test/mock/data/structure.mock.dto';
......@@ -71,7 +71,7 @@ describe('StructuresController', () => {
const mockPersonalOffersService = { delete: jest.fn() };
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [],
controllers: [StructuresController],
providers: [
......
import { HttpModule } from '@nestjs/axios';
import { HttpStatus } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { MailerModule } from '../mailer/mailer.module';
import { TempUserController } from './temp-user.controller';
import { TempUserService } from './temp-user.service';
......@@ -14,7 +14,7 @@ describe('TempUserService', () => {
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [HttpModule, MailerModule],
providers: [{ provide: TempUserService, useValue: mockTempUserService }],
controllers: [TempUserController],
......@@ -47,7 +47,6 @@ describe('TempUserService', () => {
mockTempUserService.findById.mockReturnValueOnce(tmpUser);
try {
await tempUserController.getTempUser('addq651');
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
expect(e.message).toEqual('Temp user does not exist');
......
import { HttpModule } from '@nestjs/axios';
import { HttpStatus } from '@nestjs/common';
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import * as ejs from 'ejs';
import { MailerModule } from '../mailer/mailer.module';
import { MailerService } from '../mailer/mailer.service';
......@@ -45,7 +45,7 @@ describe('TempUserService', () => {
// Set up the test module before each test
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [HttpModule, MailerModule],
providers: [
TempUserService,
......
import { HttpModule } from '@nestjs/axios';
import { HttpStatus } from '@nestjs/common';
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { Types } from 'mongoose';
import { UsersServiceMock } from '../../../test/mock/services/user.mock.service';
import { ConfigurationModule } from '../../configuration/configuration.module';
......@@ -9,7 +9,6 @@ import { Employer } from '../schemas/employer.schema';
import { EmployerService } from '../services/employer.service';
import { UsersService } from '../services/users.service';
import { EmployerController } from './employer.controller';
import { CreateEmployerDto } from '../dto/create-employer.dto';
describe('EmployerController', () => {
let employerController: EmployerController;
......@@ -32,7 +31,7 @@ describe('EmployerController', () => {
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
imports: [ConfigurationModule, HttpModule],
providers: [
{
......@@ -134,7 +133,7 @@ describe('EmployerController', () => {
const req = { user: { _id: '6036721022462b001334c4bb' }, role: 0 };
try {
await employerController.createEmployer({ name: 'Sopra' }, req);
expect;
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('Employer already exist');
expect(e.status).toBe(HttpStatus.UNPROCESSABLE_ENTITY);
......