Skip to content

Commit

Permalink
add mock api
Browse files Browse the repository at this point in the history
  • Loading branch information
oumeimaelisbihani committed Nov 14, 2024
1 parent 9786354 commit cc09272
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 13 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion libs/backend-ddd/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
23 changes: 19 additions & 4 deletions libs/backend-ddd/src/opportunity/application/opportunityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,18 +57,21 @@ 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 {
return ProgramsJson.getInstance()
}

private _getMailRepository(): MailerManager {
return { sendReturnReceipt: new BrevoMail().sendReturnReceipt }
return { sendReturnReceipt: Config.BREVO_API_ENABLED ? new BrevoMail().sendReturnReceipt : new BrevoMailTest().sendReturnReceipt }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class OpportunityFeatures {
})
}

private async _updateOpportunitySentToHub(opportunityId: OpportunityId, success: boolean): Promise<Maybe<Error>> {
private async _updateOpportunitySentToHub(opportunityId: OpportunityId, success: boolean): Promise<Maybe<Error | null>> {
return await this._opportunityRepository.update(opportunityId, { sentToOpportunityHub: success })
}

Expand Down
2 changes: 1 addition & 1 deletion libs/backend-ddd/src/opportunity/domain/spi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type OpportunityRepository = {
opportunity: OpportunityWithOperatorContactAndContactId,
opportunityAssociatedObject: OpportunityAssociatedData
) => Promise<Result<OpportunityId, Error>>
update: (dealId: OpportunityId, attributes: OpportunityUpdateAttributes) => Promise<Maybe<Error>>
update: (dealId: OpportunityId, attributes: OpportunityUpdateAttributes) => Promise<Maybe<Error | null>>
readDates: () => Promise<Result<Date[], Error>>
getDailyOpportunitiesByContactId: (contactId: number) => Promise<Result<OpportunityDetailsShort[], Error>>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const requestCreateDeal = async (name: string, attributes: DealAttributes): Prom
const updateBrevoDeal: OpportunityRepository['update'] = async (
dealId: OpportunityId,
updateAttributes: OpportunityUpdateAttributes
): Promise<Maybe<Error>> => {
): Promise<Maybe<Error | null>> => {
const brevoDeal = convertDomainToBrevoDealUpdate(updateAttributes)

return requestUpdateDeal(dealId, brevoDeal)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Result<OpportunityId, Error>> => {
return Result.ok({ id: '0' } as OpportunityId)
}

const updateBrevoDeal: OpportunityRepository['update'] = async (): Promise<Maybe<Error | null>> => {
return Maybe.of(null)
}

const getBrevoCreationDates = async (): Promise<Result<Date[], Error>> => {
return Result.ok([] as Date[])
}

const getDailyOpportunitiesByContactId = async (): Promise<Result<OpportunityDetailsShort[], Error>> => {
return Result.ok([] as OpportunityDetailsShort[])
}

export const brevoRepositoryTest: OpportunityRepository = {
create: addBrevoDeal,
update: updateBrevoDeal,
readDates: getBrevoCreationDates,
getDailyOpportunitiesByContactId: getDailyOpportunitiesByContactId
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Maybe } from 'true-myth'
import { MailerManager } from '../../../../domain/spi'

export default class BrevoMailTest {
sendReturnReceipt: MailerManager['sendReturnReceipt'] = async (): Promise<Maybe<Error> | void> => {
console.log('send email')
}
}
Original file line number Diff line number Diff line change
@@ -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<Maybe<Error>> => {
return Maybe.nothing()
}
}
Original file line number Diff line number Diff line change
@@ -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<Maybe<Error>> => {
return Maybe.nothing()
}

async reachedDailyContactTransmissionLimit(): Promise<boolean> {
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]
}
}
}

0 comments on commit cc09272

Please sign in to comment.