From cc09272854d250ebffafe2612c28b056ed6b041b Mon Sep 17 00:00:00 2001 From: oumeimaelisbihani Date: Thu, 14 Nov 2024 17:34:30 +0100 Subject: [PATCH] add mock api --- .github/workflows/playwright.yml | 7 +- libs/backend-ddd/src/config.ts | 10 ++- .../application/opportunityService.ts | 23 ++++-- .../opportunity/domain/opportunityFeatures.ts | 2 +- .../backend-ddd/src/opportunity/domain/spi.ts | 2 +- .../infrastructure/api/brevo/brevoDeal.ts | 2 +- .../api/brevo/mock/brevoDeal.ts | 28 ++++++++ .../api/brevo/mock/brevoMail.ts | 8 +++ .../infrastructure/api/mock/bpiFrance.ts | 26 +++++++ .../api/mock/placeDesEntreprises.ts | 70 +++++++++++++++++++ 10 files changed, 165 insertions(+), 13 deletions(-) create mode 100644 libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoDeal.ts create mode 100644 libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoMail.ts create mode 100644 libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/bpiFrance.ts create mode 100644 libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/placeDesEntreprises.ts diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml index 6e25e024b..068bd6833 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/playwright.yml @@ -26,11 +26,8 @@ jobs: - name: Run Playwright tests env: TEST: true - PDE_API_BASEURL: '${{secrets.PDE_API_BASEURL}}' - BREVO_DEAL_PIPELINE: '${{secrets.BREVO_DEAL_PIPELINE}}' - run: | - echo "PDE_API_BASEURL=${{ secrets.PDE_API_BASEURL }}" - npm run e2e + TIERS_API_ENABLE: false + run: npm run e2e - uses: actions/upload-artifact@v4 if: always() with: diff --git a/libs/backend-ddd/src/config.ts b/libs/backend-ddd/src/config.ts index 4bdb5f8a6..ccfe831bd 100644 --- a/libs/backend-ddd/src/config.ts +++ b/libs/backend-ddd/src/config.ts @@ -45,6 +45,14 @@ export default class Config extends ConfigCommon { } public static get BREVO_API_ENABLED(): boolean { - return !(this.getEnvValue('TIERS_API_ENABLE', 'true') !== 'true' && this.getEnvValue('BREVO_API_ENABLED', 'true') !== 'true') + return this.getEnvValue('TIERS_API_ENABLE', 'true') !== 'false' && this.getEnvValue('BREVO_API_ENABLED', 'true') !== 'true' + } + + public static get PDE_API_ENABLED(): boolean { + return this.getEnvValue('TIERS_API_ENABLE', 'true') !== 'false' && this.getEnvValue('PDE_API_ENABLED', 'true') !== 'true' + } + + public static get BPI_API_ENABLED(): boolean { + return this.getEnvValue('TIERS_API_ENABLE', 'true') !== 'false' && this.getEnvValue('BPI_API_ENABLED', 'true') !== 'true' } } diff --git a/libs/backend-ddd/src/opportunity/application/opportunityService.ts b/libs/backend-ddd/src/opportunity/application/opportunityService.ts index a00c170f3..260b14347 100644 --- a/libs/backend-ddd/src/opportunity/application/opportunityService.ts +++ b/libs/backend-ddd/src/opportunity/application/opportunityService.ts @@ -2,17 +2,29 @@ import Config from '../../config' import { OpportunityDetailsShort, OpportunityId } from '../domain/types' import OpportunityFeatures from '../domain/opportunityFeatures' import { Result } from 'true-myth' + import { brevoRepository } from '../infrastructure/api/brevo/brevoDeal' +import { brevoRepositoryTest } from '../infrastructure/api/brevo/mock/brevoDeal' + import { addBrevoContact } from '../infrastructure/api/brevo/brevoContact' +import { addBrevoContactTest } from '../infrastructure/api/brevo/mock/brevoContact' + import { OpportunityHubRepository } from '../../opportunityHub/domain/spi' + import { BpiFrance } from '../../opportunityHub/infrastructure/api/bpi/bpiFrance' +import { BpiFranceTest } from '../../opportunityHub/infrastructure/api/mock/bpiFrance' + import { ContactRepository, MailerManager, OpportunityRepository } from '../domain/spi' import { ProgramRepository } from '../../program/domain/spi' import ProgramsJson from '../../program/infrastructure/programsJson' + import BrevoMail from '../infrastructure/api/brevo/brevoMail' +import BrevoMailTest from '../infrastructure/api/brevo/mock/brevoMail' + import { PlaceDesEntreprises } from '../../opportunityHub/infrastructure/api/placedesentreprises/placeDesEntreprises' +import { PlaceDesEntreprisesTest } from '../../opportunityHub/infrastructure/api/mock/placeDesEntreprises' + import { Opportunity } from '@tee/common' -import { addBrevoContactTest } from '../infrastructure/api/brevo/mock/brevoContact' export default class OpportunityService { private _opportunityFeatures: OpportunityFeatures @@ -45,11 +57,14 @@ export default class OpportunityService { } private _getOpportunityRepository(): OpportunityRepository { - return brevoRepository + return Config.BREVO_API_ENABLED ? brevoRepository : brevoRepositoryTest } private _getOpportunityHubRepositories(): OpportunityHubRepository[] { - return [new PlaceDesEntreprises(), new BpiFrance()] + return [ + Config.PDE_API_ENABLED ? new PlaceDesEntreprises() : new PlaceDesEntreprisesTest(), + Config.BPI_API_ENABLED ? new BpiFrance() : new BpiFranceTest() + ] } private _getProgramRepository(): ProgramRepository { @@ -57,6 +72,6 @@ export default class OpportunityService { } private _getMailRepository(): MailerManager { - return { sendReturnReceipt: new BrevoMail().sendReturnReceipt } + return { sendReturnReceipt: Config.BREVO_API_ENABLED ? new BrevoMail().sendReturnReceipt : new BrevoMailTest().sendReturnReceipt } } } diff --git a/libs/backend-ddd/src/opportunity/domain/opportunityFeatures.ts b/libs/backend-ddd/src/opportunity/domain/opportunityFeatures.ts index c62a7ab64..64e08304e 100644 --- a/libs/backend-ddd/src/opportunity/domain/opportunityFeatures.ts +++ b/libs/backend-ddd/src/opportunity/domain/opportunityFeatures.ts @@ -119,7 +119,7 @@ export default class OpportunityFeatures { }) } - private async _updateOpportunitySentToHub(opportunityId: OpportunityId, success: boolean): Promise> { + private async _updateOpportunitySentToHub(opportunityId: OpportunityId, success: boolean): Promise> { return await this._opportunityRepository.update(opportunityId, { sentToOpportunityHub: success }) } diff --git a/libs/backend-ddd/src/opportunity/domain/spi.ts b/libs/backend-ddd/src/opportunity/domain/spi.ts index 00f53e463..04d627e31 100644 --- a/libs/backend-ddd/src/opportunity/domain/spi.ts +++ b/libs/backend-ddd/src/opportunity/domain/spi.ts @@ -18,7 +18,7 @@ export type OpportunityRepository = { opportunity: OpportunityWithOperatorContactAndContactId, opportunityAssociatedObject: OpportunityAssociatedData ) => Promise> - update: (dealId: OpportunityId, attributes: OpportunityUpdateAttributes) => Promise> + update: (dealId: OpportunityId, attributes: OpportunityUpdateAttributes) => Promise> readDates: () => Promise> getDailyOpportunitiesByContactId: (contactId: number) => Promise> } diff --git a/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/brevoDeal.ts b/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/brevoDeal.ts index 74fb1659e..ccd205577 100644 --- a/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/brevoDeal.ts +++ b/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/brevoDeal.ts @@ -67,7 +67,7 @@ const requestCreateDeal = async (name: string, attributes: DealAttributes): Prom const updateBrevoDeal: OpportunityRepository['update'] = async ( dealId: OpportunityId, updateAttributes: OpportunityUpdateAttributes -): Promise> => { +): Promise> => { const brevoDeal = convertDomainToBrevoDealUpdate(updateAttributes) return requestUpdateDeal(dealId, brevoDeal) diff --git a/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoDeal.ts b/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoDeal.ts new file mode 100644 index 000000000..aaadc270a --- /dev/null +++ b/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoDeal.ts @@ -0,0 +1,28 @@ +import { Maybe, Result } from 'true-myth' +import { OpportunityRepository } from '../../../../domain/spi' +import { OpportunityDetailsShort, OpportunityId } from '../../../../domain/types' + +// "Opportunities" are called "Deals" in Brevo + +const addBrevoDeal: OpportunityRepository['create'] = async (): Promise> => { + return Result.ok({ id: '0' } as OpportunityId) +} + +const updateBrevoDeal: OpportunityRepository['update'] = async (): Promise> => { + return Maybe.of(null) +} + +const getBrevoCreationDates = async (): Promise> => { + return Result.ok([] as Date[]) +} + +const getDailyOpportunitiesByContactId = async (): Promise> => { + return Result.ok([] as OpportunityDetailsShort[]) +} + +export const brevoRepositoryTest: OpportunityRepository = { + create: addBrevoDeal, + update: updateBrevoDeal, + readDates: getBrevoCreationDates, + getDailyOpportunitiesByContactId: getDailyOpportunitiesByContactId +} diff --git a/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoMail.ts b/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoMail.ts new file mode 100644 index 000000000..970a9e56c --- /dev/null +++ b/libs/backend-ddd/src/opportunity/infrastructure/api/brevo/mock/brevoMail.ts @@ -0,0 +1,8 @@ +import { Maybe } from 'true-myth' +import { MailerManager } from '../../../../domain/spi' + +export default class BrevoMailTest { + sendReturnReceipt: MailerManager['sendReturnReceipt'] = async (): Promise | void> => { + console.log('send email') + } +} diff --git a/libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/bpiFrance.ts b/libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/bpiFrance.ts new file mode 100644 index 000000000..52b98603b --- /dev/null +++ b/libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/bpiFrance.ts @@ -0,0 +1,26 @@ +import { Maybe } from 'true-myth' +import { AxiosInstance } from 'axios' +import OpportunityHubAbstract from '../opportunityHubAbstract' +import { Operators } from '@tee/data' + +export class BpiFranceTest extends OpportunityHubAbstract { + protected _axios: AxiosInstance + protected readonly _baseUrl = 'https://bpifrance.my.salesforce.com/services' + protected readonly _operatorNames: Operators[] = ['Bpifrance'] + + constructor() { + super() + this._axios = { + request: async () => { + return { + status: 200, + data: {} + } + } + } as unknown as AxiosInstance + } + + public transmitOpportunity = async (): Promise> => { + return Maybe.nothing() + } +} diff --git a/libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/placeDesEntreprises.ts b/libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/placeDesEntreprises.ts new file mode 100644 index 000000000..256f3510b --- /dev/null +++ b/libs/backend-ddd/src/opportunityHub/infrastructure/api/mock/placeDesEntreprises.ts @@ -0,0 +1,70 @@ +import { Operators } from '@tee/data' +import { Objective } from '@tee/common' +import { Maybe } from 'true-myth' +import { AxiosInstance } from 'axios' +import Config from '../../../../config' +import { Subject, subjectToIdMapping } from '../placedesentreprises/types' +import OpportunityHubAbstract from '../opportunityHubAbstract' + +export class PlaceDesEntreprisesTest extends OpportunityHubAbstract { + protected readonly _baseUrl = Config.PDE_API_BASEURL + protected _axios: AxiosInstance + protected readonly _operatorNames = [] // warning, invalid but never used since we override all possible external uses of this value right below + constructor() { + super() + this._axios = { + request: async () => { + return { + status: 200, + data: {} + } + } + } as unknown as AxiosInstance + } + + override get operatorNames(): Operators[] | Error { + return new Error('Operator List non valid for Place des entreprises') + } + + override support = () => { + return true + } + + override shouldTransmit = async () => { + return true + } + + public transmitOpportunity = async (): Promise> => { + return Maybe.nothing() + } + + async reachedDailyContactTransmissionLimit(): Promise { + return false + } + + private _objectiveToPdeSubjectMapping: { [key in Objective]: Subject } = { + [Objective.EnvironmentalImpact]: Subject.DemarcheEcologie, + [Objective.EnergyPerformance]: Subject.Energie, + [Objective.WaterConsumption]: Subject.Eau, + [Objective.BuildingRenovation]: Subject.Energie, + [Objective.SustainableMobility]: Subject.TransportMobilite, + [Objective.WasteManagement]: Subject.Dechets, + [Objective.EcoDesign]: Subject.DemarcheEcologie, + [Objective.TrainOrRecruit]: Subject.BilanRSE, + [Objective.MakeSavings]: Subject.DemarcheEcologie, + [Objective.DurablyInvest]: Subject.DemarcheEcologie, + [Objective.Biodiversity]: Subject.DemarcheEcologie, + [Objective.UnknownYet]: Subject.DemarcheEcologie + } + + subjectMapping(programObjectives: Objective[]): number { + const defaultSubject = Subject.DemarcheEcologie + if (programObjectives.length === 1) { + const objective = programObjectives[0] + const subjectKey = this._objectiveToPdeSubjectMapping[objective] + return subjectToIdMapping[subjectKey] + } else { + return subjectToIdMapping[defaultSubject] + } + } +}