Skip to content

Commit

Permalink
convert RomeSearch and AppellationSearch to transactional use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Mar 28, 2022
1 parent 92c1f1b commit 7244e67
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 27 deletions.
14 changes: 10 additions & 4 deletions back/src/__tests__/unit/useCases/AppellationSearch.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { createInMemoryUow } from "../../../adapters/primary/config";
import { InMemoryUowPerformer } from "../../../adapters/secondary/InMemoryUowPerformer";
import { RomeRepository } from "../../../domain/rome/ports/RomeRepository";
import { InMemoryRomeRepository } from "../../../adapters/secondary/InMemoryRomeRepository";
import { AppellationSearch } from "../../../domain/rome/useCases/AppellationSearch";
import { AppellationMatchDto } from "../../../shared/romeAndAppellationDtos/romeAndAppellation.dto";

describe("AppellationSearch", () => {
let gateway: RomeRepository;
let romeRepo: RomeRepository;

beforeEach(() => {
gateway = new InMemoryRomeRepository();
romeRepo = new InMemoryRomeRepository();
});

const createUseCase = () => {
return new AppellationSearch(gateway);
const uowPerformer = new InMemoryUowPerformer({
...createInMemoryUow(),
romeRepo,
});
return new AppellationSearch(uowPerformer);
};

it("returns the list of found matches with ranges", async () => {
Expand All @@ -34,7 +40,7 @@ describe("AppellationSearch", () => {
const mockSearchMetierFn = jest.fn();
const mockSearchAppellationFn = jest.fn();
const mockAppellationToCodeMetier = jest.fn();
gateway = {
romeRepo = {
searchRome: mockSearchMetierFn,
searchAppellation: mockSearchAppellationFn,
appellationToCodeMetier: mockAppellationToCodeMetier,
Expand Down
9 changes: 7 additions & 2 deletions back/src/__tests__/unit/useCases/RomeSearch.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { createInMemoryUow } from "../../../adapters/primary/config";
import { InMemoryRomeRepository } from "../../../adapters/secondary/InMemoryRomeRepository";
import { InMemoryUowPerformer } from "../../../adapters/secondary/InMemoryUowPerformer";
import { RomeSearch } from "../../../domain/rome/useCases/RomeSearch";
import { RomeDto } from "../../../shared/romeAndAppellationDtos/romeAndAppellation.dto";

const prepareUseCase = () => {
const romeRepo = new InMemoryRomeRepository();
const useCase = new RomeSearch(romeRepo);
return useCase;
const uowPerformer = new InMemoryUowPerformer({
...createInMemoryUow(),
romeRepo,
});
return new RomeSearch(uowPerformer);
};
describe("RomeSearch", () => {
it("returns the list of found matches with ranges", async () => {
Expand Down
6 changes: 4 additions & 2 deletions back/src/adapters/primary/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ const createRomeRepostiory = async (

export type InMemoryUnitOfWork = ReturnType<typeof createInMemoryUow>;
export const createInMemoryUow = (repositories?: Repositories) => ({
romeRepo: repositories?.rome ?? new InMemoryRomeRepository(),
outboxRepo:
(repositories?.outbox as InMemoryOutboxRepository) ??
new InMemoryOutboxRepository(),
Expand Down Expand Up @@ -381,6 +382,7 @@ export const createInMemoryUow = (repositories?: Repositories) => ({
const _isAssignable = (inMemory: InMemoryUnitOfWork): UnitOfWork => inMemory;

export const createPgUow = (client: PoolClient): UnitOfWork => ({
romeRepo: new PgRomeRepository(client),
outboxRepo: new PgOutboxRepository(client),
agencyRepo: new PgAgencyRepository(client),
formEstablishmentRepo: new PgFormEstablishmentRepository(client),
Expand Down Expand Up @@ -567,8 +569,8 @@ const createUseCases = (
getSiret,

// romes
appellationSearch: new AppellationSearch(repositories.rome),
romeSearch: new RomeSearch(repositories.rome),
appellationSearch: new AppellationSearch(uowPerformer),
romeSearch: new RomeSearch(uowPerformer),

// agencies
listAgencies: new ListAgencies(repositories.agency),
Expand Down
2 changes: 2 additions & 0 deletions back/src/domain/core/ports/UnitOfWork.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AgencyRepository } from "../../immersionApplication/ports/AgencyRepository";
import { FormEstablishmentRepository } from "../../immersionOffer/ports/FormEstablishmentRepository";
import { ImmersionOfferRepository } from "../../immersionOffer/ports/ImmersionOfferRepository";
import { RomeRepository } from "../../rome/ports/RomeRepository";
import { GetFeatureFlags } from "./GetFeatureFlags";
import { OutboxRepository } from "./OutboxRepository";
import { ImmersionApplicationRepository } from "../../immersionApplication/ports/ImmersionApplicationRepository";
Expand All @@ -9,6 +10,7 @@ import { EstablishmentExportQueries } from "../../establishment/ports/Establishm
import { PostalCodeDepartmentRegionQueries } from "../../generic/geo/ports/PostalCodeDepartmentRegionQueries";

export type UnitOfWork = {
romeRepo: RomeRepository;
outboxRepo: OutboxRepository;
agencyRepo: AgencyRepository;
formEstablishmentRepo: FormEstablishmentRepository;
Expand Down
23 changes: 14 additions & 9 deletions back/src/domain/rome/useCases/AppellationSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ import { AppellationMatchDto } from "../../../shared/romeAndAppellationDtos/rome
import { zTrimmedString } from "../../../shared/zodUtils";
import { createLogger } from "../../../utils/logger";
import { findMatchRanges } from "../../../utils/textSearch";
import { UseCase } from "../../core/UseCase";
import { RomeRepository } from "../ports/RomeRepository";
import { UnitOfWork, UnitOfWorkPerformer } from "../../core/ports/UnitOfWork";
import { TransactionalUseCase } from "../../core/UseCase";

const logger = createLogger(__filename);

const MIN_SEARCH_TEXT_LENGTH = 3;
export class AppellationSearch extends UseCase<string, AppellationMatchDto[]> {
public constructor(readonly romeRepository: RomeRepository) {
super();

export class AppellationSearch extends TransactionalUseCase<
string,
AppellationMatchDto[]
> {
public constructor(uowPerformer: UnitOfWorkPerformer) {
super(uowPerformer);
}

inputSchema = zTrimmedString;

public async _execute(searchText: string): Promise<AppellationMatchDto[]> {
public async _execute(
searchText: string,
uow: UnitOfWork,
): Promise<AppellationMatchDto[]> {
if (searchText.length <= MIN_SEARCH_TEXT_LENGTH) return [];

const appellations = await this.romeRepository.searchAppellation(
searchText,
);
const appellations = await uow.romeRepo.searchAppellation(searchText);

const appellationsWithMatch: AppellationMatchDto[] = appellations.map(
(appellation) => ({
Expand Down
17 changes: 10 additions & 7 deletions back/src/domain/rome/useCases/RomeSearch.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { RomeDto } from "../../../shared/romeAndAppellationDtos/romeAndAppellation.dto";
import { zTrimmedString } from "../../../shared/zodUtils";
import { UseCase } from "../../core/UseCase";
import { RomeRepository } from "../ports/RomeRepository";
import { UnitOfWork, UnitOfWorkPerformer } from "../../core/ports/UnitOfWork";
import { TransactionalUseCase } from "../../core/UseCase";

const MIN_SEARCH_TEXT_LENGTH = 3;

export class RomeSearch extends UseCase<string, RomeDto[]> {
public constructor(readonly romeRepository: RomeRepository) {
super();
export class RomeSearch extends TransactionalUseCase<string, RomeDto[]> {
constructor(uowPerformer: UnitOfWorkPerformer) {
super(uowPerformer);
}

inputSchema = zTrimmedString;

public async _execute(searchText: string): Promise<RomeDto[]> {
public async _execute(
searchText: string,
uow: UnitOfWork,
): Promise<RomeDto[]> {
if (searchText.length <= MIN_SEARCH_TEXT_LENGTH) return [];
return await this.romeRepository.searchRome(searchText);
return await uow.romeRepo.searchRome(searchText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
MatchRangeDto,
} from "./romeAndAppellation.dto";


const codeRomeRegex = /^[A-N]\d{4}$/;
export const codeRomeSchema: z.Schema<RomeCode> = z
.string()
Expand Down
1 change: 0 additions & 1 deletion back/src/utils/notifyDiscord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const notifyDiscord = (

if (!discordWebhookUrl) return;


const content = rawContent.slice(0, discordSizeLimit);
// This is intentionaly not awaited following a fire and forget logic.
axios.post(
Expand Down
1 change: 0 additions & 1 deletion front/src/app/Profession/AppellationAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export const AppellationAutocomplete = ({
noOptionsText={searchTerm ? noOptionText : "Saisissez un métier"}
getOptionLabel={(option: Option) => option.value.appellationLabel}
renderOption={(props, option) => (
// <li {...props}>{option.romeLabel}</li>
<li {...props}>
<StringWithHighlights
description={option.description}
Expand Down

0 comments on commit 7244e67

Please sign in to comment.