Skip to content

Commit

Permalink
ajout bilan en tonnes dans le redirection post reception situation NGC
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojciech WOJCIK committed Oct 7, 2024
1 parent b6bd003 commit dd75d66
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/infrastructure/api/inscription.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ApiOkResponse,
ApiExtraModels,
ApiOperation,
ApiFoundResponse,
} from '@nestjs/swagger';
import { ProspectSubmitAPI } from './types/utilisateur/onboarding/prospectSubmitAPI';
import { ValidateCodeAPI } from './types/utilisateur/onboarding/validateCodeAPI';
Expand Down Expand Up @@ -77,12 +78,17 @@ export class InscriptionController extends GenericControler {

@ApiBody({ type: SituationNGCAPI })
@Post('bilan/importFromNGC')
@Redirect('https://FRONT/creation-compte?situatio_id=1234', 302)
@ApiFoundResponse({
description:
"Redirige vers la page d'inscription avec 2 param d'URL : situation_id et bilan_tonnes. L'url a la forme DOMAINE/creation-compte?situation_id=XYZ&bilan_tonnes=8",
})
@UseGuards(ThrottlerGuard)
async importFromNGC(@Body() body: SituationNGCAPI) {
const id = await this.bilanUsecase.importSituationNGC(body.situation);
const result = await this.bilanUsecase.importSituationNGC(body.situation);
return {
url: `${App.getBaseURLFront()}/creation-compte?situation_id=${id}`,
url: `${App.getBaseURLFront()}/creation-compte?situation_id=${
result.id_situtation
}&bilan_tonnes=${result.bilan_tonnes}`,
};
}
}
24 changes: 21 additions & 3 deletions src/usecase/bilan.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { Injectable } from '@nestjs/common';
import { SituationNGCRepository } from '../infrastructure/repository/bilan.repository';
import { NGCCalculator } from '../infrastructure/ngc/NGCCalculator';

@Injectable()
export class BilanUsecase {
constructor(private bilanRepository: SituationNGCRepository) {}
constructor(
private bilanRepository: SituationNGCRepository,
private ngcCaclulator: NGCCalculator,
) {}

async importSituationNGC(situation: object): Promise<string> {
return await this.bilanRepository.createSituation(situation);
async importSituationNGC(
situation: object,
): Promise<{ id_situtation: string; bilan_tonnes: number }> {
const id_situtation = await this.bilanRepository.createSituation(situation);
let bilan = { bilan_carbone_annuel: 8000 };
try {
bilan = this.ngcCaclulator.computeBilanFromSituation(situation);
} catch (error) {
console.error(
`Erreur calcul bilan carbone utilisateur NGC, id_situation : ${id_situtation}`,
);
}
return {
id_situtation: id_situtation,
bilan_tonnes: Math.round(bilan.bilan_carbone_annuel / 1000),
};
}
}
24 changes: 23 additions & 1 deletion test/integration/api/bilan.controller.int-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,29 @@ describe('/bilan (API test)', () => {
expect(response.headers.location).toEqual(
`${App.getBaseURLFront()}/creation-compte?situation_id=${
situationDB[0].id
}`,
}&bilan_tonnes=10`,
);
});
it.only('POST /bilan/importFromNGC - creates new situation alors que erreur de contenu, 8 tonnes par défaut ^^', async () => {
// WHEN
const response = await TestUtil.POST('/bilan/importFromNGC').send({
situation: {
'C est vraiement pas bon': 'dfsgsdg',
},
});

//THEN
expect(response.status).toBe(302);
const situationDB = await TestUtil.prisma.situationNGC.findMany({});
expect(situationDB).toHaveLength(1);
expect(situationDB[0].situation).toStrictEqual({
'C est vraiement pas bon': 'dfsgsdg',
});

expect(response.headers.location).toEqual(
`${App.getBaseURLFront()}/creation-compte?situation_id=${
situationDB[0].id
}&bilan_tonnes=8`,
);
});
});

0 comments on commit dd75d66

Please sign in to comment.