Skip to content

Commit

Permalink
throw error when broadcast request already in process
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-duport committed Nov 5, 2024
1 parent ab4d01b commit 3ac9146
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
formatDuration,
intervalToDuration,
isBefore,
subMinutes,
} from "date-fns";
import fr from "date-fns/locale/fr";
import {
Expand Down Expand Up @@ -56,6 +57,17 @@ export const makeBroadcastConventionAgain = createTransactionalUseCase<
now: deps.timeGateway.now(),
});

const lastUnpublishedBroadcastRequest =
await uow.outboxRepository.getLastUnpublishedConventionBroadcastRequestedEventByConventionId(
conventionId,
);
if (
lastUnpublishedBroadcastRequest &&
new Date(lastUnpublishedBroadcastRequest?.occurredAt) <=
subMinutes(deps.timeGateway.now(), 30)
)
throw errors.broadcastFeedback.requestAlreadyInProcess();

const broadcastConventionAgainEvent = deps.createNewEvent({
topic: "ConventionBroadcastRequested",
payload: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
expectArraysToMatch,
expectPromiseToFailWithError,
} from "shared";
import { makeCreateNewEvent } from "../../../core/events/ports/EventBus";
import { DomainEvent } from "../../../core/events/events";
import {
CreateNewEvent,
makeCreateNewEvent,
} from "../../../core/events/ports/EventBus";
import {
BroadcastFeedback,
broadcastToPeServiceName,
Expand Down Expand Up @@ -51,13 +55,14 @@ describe("BroadcastConventionAgain", () => {
let broadcastConventionAgain: BroadcastConventionAgain;
let uow: InMemoryUnitOfWork;
let timeGateway: TimeGateway;
let createNewEvent: CreateNewEvent;

beforeEach(async () => {
uow = createInMemoryUow();
const uowPerformer = new InMemoryUowPerformer(uow);
const uuidGenerator = new TestUuidGenerator();
timeGateway = new CustomTimeGateway();
const createNewEvent = makeCreateNewEvent({
createNewEvent = makeCreateNewEvent({
uuidGenerator,
timeGateway,
});
Expand Down Expand Up @@ -199,5 +204,25 @@ describe("BroadcastConventionAgain", () => {
}),
);
});

it("throws requestAlreadyInProcess when user request broadcast again before last broadcast request was published", async () => {
const lastUnpublishedBroadcastRequestEventDate = subHours(
addMinutes(timeGateway.now(), 12),
1,
).toISOString();
const lastUnpublishedBroadcastRequestEvent: DomainEvent = createNewEvent({
topic: "ConventionBroadcastRequested",
occurredAt: lastUnpublishedBroadcastRequestEventDate,
status: "in-process",
payload: { convention, triggeredBy: null },
});

uow.outboxRepository.save(lastUnpublishedBroadcastRequestEvent);

await expectPromiseToFailWithError(
broadcastConventionAgain.execute({ conventionId }, adminUser),
errors.broadcastFeedback.requestAlreadyInProcess(),
);
});
});
});
19 changes: 19 additions & 0 deletions back/src/domains/core/events/adapters/InMemoryOutboxRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { values } from "ramda";
import { ConventionId } from "shared";
import { createLogger } from "../../../../utils/logger";
import { DomainEvent, EventStatus } from "../events";
import { OutboxRepository } from "../ports/OutboxRepository";
Expand Down Expand Up @@ -35,4 +36,22 @@ export class InMemoryOutboxRepository implements OutboxRepository {
};
});
}

public async getLastUnpublishedConventionBroadcastRequestedEventByConventionId(
conventionId: ConventionId,
): Promise<DomainEvent | undefined> {
const filteredEvents = values(this._events).filter(
(event) =>
event.topic === "ConventionBroadcastRequested" &&
event.status !== "published" &&
event.payload.convention.id === conventionId,
);

const sortedEvents = filteredEvents.sort(
(a, b) =>
new Date(b.occurredAt).getTime() - new Date(a.occurredAt).getTime(),
);

return sortedEvents[0] || undefined;
}
}
4 changes: 4 additions & 0 deletions back/src/domains/core/events/ports/OutboxRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ConventionId } from "shared";
import { DomainEvent, EventStatus } from "../events";

export interface OutboxRepository {
countAllEvents(params: { status: EventStatus }): Promise<number>;
save: (event: DomainEvent) => Promise<void>;
markEventsAsInProcess: (events: DomainEvent[]) => Promise<void>;
getLastUnpublishedConventionBroadcastRequestedEventByConventionId: (
conventionId: ConventionId,
) => Promise<DomainEvent | undefined>;
}
4 changes: 4 additions & 0 deletions shared/src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ export const errors = {
showGMT: true,
})}. Merci d'essayer à nouveau dans ${params.formattedWaitingTime}.`,
),
requestAlreadyInProcess: () =>
new TooManyRequestApiError(
"Il y a déjà une synchronisation en cours. Cela peut prendre quelques instants. Merci de réessayer plus tard.",
),
},
discussion: {
badSiretFilter: () =>
Expand Down

0 comments on commit 3ac9146

Please sign in to comment.