Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save notifications last ones #265

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions back/src/adapters/primary/config/createUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export const createUseCases = (
quarantinedTopics: config.quarantinedTopics,
});
const saveNotificationAndRelatedEvent = makeSaveNotificationAndRelatedEvent(
createNewEvent,
uuidGenerator,
gateways.timeGateway,
createNewEvent,
);
const addFormEstablishment = new AddFormEstablishment(
uowPerformer,
Expand All @@ -134,7 +134,7 @@ export const createUseCases = (
),
notifyIcUserAgencyRightChanged: new NotifyIcUserAgencyRightChanged(
uowPerformer,
gateways.notification,
saveNotificationAndRelatedEvent,
),
getIcUsers: new GetInclusionConnectedUsers(uowPerformer),
getUserAgencyDashboardUrl: new GetInclusionConnectedUser(
Expand Down Expand Up @@ -282,7 +282,7 @@ export const createUseCases = (
),
requestEditFormEstablishment: new RequestEditFormEstablishment(
uowPerformer,
gateways.notification,
saveNotificationAndRelatedEvent,
gateways.timeGateway,
makeGenerateEditFormEstablishmentUrl(
config,
Expand Down Expand Up @@ -315,7 +315,8 @@ export const createUseCases = (
privateListAgencies: new PrivateListAgencies(uowPerformer),
getAgencyPublicInfoById: new GetAgencyPublicInfoById(uowPerformer),
sendEmailWhenAgencyIsActivated: new SendEmailWhenAgencyIsActivated(
gateways.notification,
uowPerformer,
saveNotificationAndRelatedEvent,
),
// METABASE
...dashboardUseCases(gateways.dashboardGateway, gateways.timeGateway),
Expand Down Expand Up @@ -381,7 +382,8 @@ export const createUseCases = (
config,
),
deliverRenewedMagicLink: new DeliverRenewedMagicLink(
gateways.notification,
uowPerformer,
saveNotificationAndRelatedEvent,
),
notifyConfirmationEstablishmentCreated:
new NotifyConfirmationEstablishmentCreated(
Expand All @@ -406,7 +408,8 @@ export const createUseCases = (
gateways.timeGateway,
),
shareConventionByEmail: new ShareApplicationLinkByEmail(
gateways.notification,
uowPerformer,
saveNotificationAndRelatedEvent,
),
addAgency: new AddAgency(uowPerformer, createNewEvent),
updateAgencyStatus: new UpdateAgencyStatus(uowPerformer, createNewEvent),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
buildTestApp,
InMemoryGateways,
} from "../../../../_testBuilders/buildTestApp";
import { processEventsForEmailToBeSent } from "../../../../_testBuilders/processEventsForEmailToBeSent";
import { BasicEventCrawler } from "../../../secondary/core/EventCrawlerImplementations";
import { AppConfig } from "../../config/appConfig";
import { InMemoryUnitOfWork } from "../../config/uowConfig";
Expand Down Expand Up @@ -138,7 +139,7 @@ describe(`/${agenciesRoute} route`, () => {
).toBe("active");
expect(inMemoryUow.outboxRepository.events).toHaveLength(1);

await eventCrawler.processNewEvents();
await processEventsForEmailToBeSent(eventCrawler);
expect(gateways.notification.getSentEmails()).toHaveLength(1);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
buildTestApp,
InMemoryGateways,
} from "../../../../_testBuilders/buildTestApp";
import { processEventsForEmailToBeSent } from "../../../../_testBuilders/processEventsForEmailToBeSent";
import {
GenerateConventionJwt,
makeGenerateJwtES256,
Expand Down Expand Up @@ -85,7 +86,7 @@ describe("Magic link renewal flow", () => {

expect(response.status).toBe(200);

await eventCrawler.processNewEvents();
await processEventsForEmailToBeSent(eventCrawler);

const sentEmails = gateways.notification.getSentEmails();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class RenewConventionMagicLink extends TransactionalUseCase<
conventionStatusLink: await makeMagicShortLink(
frontRoutes.conventionStatusDashboard,
),
conventionId: applicationId,
},
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import { z } from "zod";
import { AgencyDto, agencySchema } from "shared";
import { UseCase } from "../../core/UseCase";
import { NotificationGateway } from "../../generic/notifications/ports/NotificationGateway";
import { UnitOfWork, UnitOfWorkPerformer } from "../../core/ports/UnitOfWork";
import { TransactionalUseCase } from "../../core/UseCase";
import { SaveNotificationAndRelatedEvent } from "../../generic/notifications/entities/Notification";

type WithAgency = { agency: AgencyDto };

export class SendEmailWhenAgencyIsActivated extends UseCase<WithAgency> {
export class SendEmailWhenAgencyIsActivated extends TransactionalUseCase<WithAgency> {
inputSchema = z.object({ agency: agencySchema });

constructor(private readonly notificationGateway: NotificationGateway) {
super();
constructor(
uowPerformer: UnitOfWorkPerformer,
private readonly saveNotificationAndRelatedEvent: SaveNotificationAndRelatedEvent,
) {
super(uowPerformer);
}

public async _execute({ agency }: WithAgency): Promise<void> {
await this.notificationGateway.sendEmail({
type: "AGENCY_WAS_ACTIVATED",
recipients: agency.validatorEmails,
params: {
agencyName: agency.name,
agencyLogoUrl: agency.logoUrl,
public async _execute(
{ agency }: WithAgency,
uow: UnitOfWork,
): Promise<void> {
await this.saveNotificationAndRelatedEvent(uow, {
kind: "email",
templatedContent: {
type: "AGENCY_WAS_ACTIVATED",
recipients: agency.validatorEmails,
params: {
agencyName: agency.name,
agencyLogoUrl: agency.logoUrl,
},
},
followedIds: {
agencyId: agency.id,
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
import { AgencyDtoBuilder } from "shared";
import { InMemoryNotificationGateway } from "../../../adapters/secondary/notificationGateway/InMemoryNotificationGateway";
import { SendEmailWhenAgencyIsActivated } from "../../../domain/convention/useCases/SendEmailWhenAgencyIsActivated";
import { makeExpectSavedNotificationsAndEvents } from "../../../_testBuilders/makeExpectSavedNotificationsAndEvents";
import { createInMemoryUow } from "../../../adapters/primary/config/uowConfig";
import { CustomTimeGateway } from "../../../adapters/secondary/core/TimeGateway/CustomTimeGateway";
import { UuidV4Generator } from "../../../adapters/secondary/core/UuidGeneratorImplementations";
import { InMemoryUowPerformer } from "../../../adapters/secondary/InMemoryUowPerformer";
import { makeSaveNotificationAndRelatedEvent } from "../../generic/notifications/entities/Notification";
import { SendEmailWhenAgencyIsActivated } from "./SendEmailWhenAgencyIsActivated";

describe("SendEmailWhenAgencyIsActivated", () => {
it("Sends an email to validators with agency name", async () => {
// Prepare
const notificationGateway = new InMemoryNotificationGateway();
const useCase = new SendEmailWhenAgencyIsActivated(notificationGateway);
const uow = createInMemoryUow();
const uowPerformer = new InMemoryUowPerformer(uow);
const expectSavedNotificationsAndEvents =
makeExpectSavedNotificationsAndEvents(
uow.notificationRepository,
uow.outboxRepository,
);
const timeGateway = new CustomTimeGateway();
const uuidGenerator = new UuidV4Generator();
const saveNotificationAndRelatedEvent = makeSaveNotificationAndRelatedEvent(
uuidGenerator,
timeGateway,
);
const useCase = new SendEmailWhenAgencyIsActivated(
uowPerformer,
saveNotificationAndRelatedEvent,
);
const updatedAgency = AgencyDtoBuilder.create()
.withValidatorEmails(["toto@email.com"])
.withName("just-activated-agency")
.withLogoUrl("https://logo.com")
.build();

// Act
await useCase.execute({ agency: updatedAgency });

// Assert
const sentEmails = notificationGateway.getSentEmails();
expect(sentEmails).toHaveLength(1);

expect(sentEmails[0].type).toBe("AGENCY_WAS_ACTIVATED");
expect(sentEmails[0].params).toEqual({
agencyName: "just-activated-agency",
expectSavedNotificationsAndEvents({
emails: [
{
type: "AGENCY_WAS_ACTIVATED",
recipients: ["toto@email.com"],
params: {
agencyName: "just-activated-agency",
agencyLogoUrl: "https://logo.com",
},
},
],
});
expect(sentEmails[0].recipients).toEqual(["toto@email.com"]);
});
});
37 changes: 24 additions & 13 deletions back/src/domain/convention/useCases/ShareApplicationLinkByEmail.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { ShareLinkByEmailDto, shareLinkByEmailSchema } from "shared";
import { UseCase } from "../../core/UseCase";
import { NotificationGateway } from "../../generic/notifications/ports/NotificationGateway";
import { UnitOfWork, UnitOfWorkPerformer } from "../../core/ports/UnitOfWork";
import { TransactionalUseCase } from "../../core/UseCase";
import { SaveNotificationAndRelatedEvent } from "../../generic/notifications/entities/Notification";

export class ShareApplicationLinkByEmail extends UseCase<ShareLinkByEmailDto> {
constructor(private readonly notificationGateway: NotificationGateway) {
super();
export class ShareApplicationLinkByEmail extends TransactionalUseCase<ShareLinkByEmailDto> {
constructor(
uowPerformer: UnitOfWorkPerformer,
private readonly saveNotificationAndRelatedEvent: SaveNotificationAndRelatedEvent,
) {
super(uowPerformer);
}
inputSchema = shareLinkByEmailSchema;

public async _execute(params: ShareLinkByEmailDto): Promise<void> {
await this.notificationGateway.sendEmail({
type: "SHARE_DRAFT_CONVENTION_BY_LINK",
recipients: [params.email],
params: {
internshipKind: params.internshipKind,
additionalDetails: params.details,
conventionFormUrl: params.conventionLink,
public async _execute(
params: ShareLinkByEmailDto,
uow: UnitOfWork,
): Promise<void> {
await this.saveNotificationAndRelatedEvent(uow, {
kind: "email",
templatedContent: {
type: "SHARE_DRAFT_CONVENTION_BY_LINK",
recipients: [params.email],
params: {
internshipKind: params.internshipKind,
additionalDetails: params.details,
conventionFormUrl: params.conventionLink,
},
},
followedIds: {},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { InMemoryNotificationRepository } from "../../../../adapters/secondary/I
import { InMemoryShortLinkQuery } from "../../../../adapters/secondary/InMemoryShortLinkQuery";
import { InMemoryUowPerformer } from "../../../../adapters/secondary/InMemoryUowPerformer";
import { DeterministShortLinkIdGeneratorGateway } from "../../../../adapters/secondary/shortLinkIdGeneratorGateway/DeterministShortLinkIdGeneratorGateway";
import { makeCreateNewEvent } from "../../../core/eventBus/EventBus";
import { ShortLinkId } from "../../../core/ports/ShortLinkQuery";
import {
EmailNotification,
Expand Down Expand Up @@ -57,12 +56,7 @@ describe("Add Convention Notifications", () => {
.build();
shortLinkGenerator = new DeterministShortLinkIdGeneratorGateway();
const uuidGenerator = new UuidV4Generator();
const createNewEvent = makeCreateNewEvent({
timeGateway,
uuidGenerator,
});
const saveNotificationAndRelatedEvent = makeSaveNotificationAndRelatedEvent(
createNewEvent,
uuidGenerator,
timeGateway,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
import { z } from "zod";
import { InternshipKind, internshipKindSchema } from "shared";
import { UseCase } from "../../../core/UseCase";
import { NotificationGateway } from "../../../generic/notifications/ports/NotificationGateway";
import {
conventionIdSchema,
InternshipKind,
internshipKindSchema,
} from "shared";
import {
UnitOfWork,
UnitOfWorkPerformer,
} from "../../../core/ports/UnitOfWork";
import { TransactionalUseCase } from "../../../core/UseCase";
import { SaveNotificationAndRelatedEvent } from "../../../generic/notifications/entities/Notification";

// prettier-ignore
export type RenewMagicLinkPayload = {
internshipKind:InternshipKind
emails:string[]
magicLink:string,
conventionStatusLink:string
conventionStatusLink: string,
conventionId?: string,
}
export const renewMagicLinkPayloadSchema: z.Schema<RenewMagicLinkPayload> =
z.object({
internshipKind: internshipKindSchema,
emails: z.array(z.string()),
magicLink: z.string(),
conventionStatusLink: z.string(),
conventionId: conventionIdSchema.optional(),
});

export class DeliverRenewedMagicLink extends UseCase<RenewMagicLinkPayload> {
constructor(private readonly notificationGateway: NotificationGateway) {
super();
export class DeliverRenewedMagicLink extends TransactionalUseCase<RenewMagicLinkPayload> {
constructor(
uowPerformer: UnitOfWorkPerformer,
private readonly saveNotificationAndRelatedEvent: SaveNotificationAndRelatedEvent,
) {
super(uowPerformer);
}

inputSchema = renewMagicLinkPayloadSchema;

public async _execute({
emails,
magicLink,
conventionStatusLink,
internshipKind,
}: RenewMagicLinkPayload): Promise<void> {
await this.notificationGateway.sendEmail({
type: "MAGIC_LINK_RENEWAL",
recipients: emails,
params: { internshipKind, magicLink, conventionStatusLink },
public async _execute(
{
emails,
magicLink,
conventionStatusLink,
internshipKind,
conventionId,
}: RenewMagicLinkPayload,
uow: UnitOfWork,
): Promise<void> {
await this.saveNotificationAndRelatedEvent(uow, {
kind: "email",
templatedContent: {
type: "MAGIC_LINK_RENEWAL",
recipients: emails,
params: { internshipKind, magicLink, conventionStatusLink },
},
followedIds: {
conventionId,
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { CustomTimeGateway } from "../../../../adapters/secondary/core/TimeGatew
import { UuidV4Generator } from "../../../../adapters/secondary/core/UuidGeneratorImplementations";
import { InMemoryUowPerformer } from "../../../../adapters/secondary/InMemoryUowPerformer";
import { DeterministShortLinkIdGeneratorGateway } from "../../../../adapters/secondary/shortLinkIdGeneratorGateway/DeterministShortLinkIdGeneratorGateway";
import { makeCreateNewEvent } from "../../../core/eventBus/EventBus";
import { makeShortLinkUrl } from "../../../core/ShortLink";
import {
EmailNotification,
Expand Down Expand Up @@ -60,9 +59,7 @@ describe("NotifyAllActorsOfFinalApplicationValidation", () => {
shortLinkIdGeneratorGateway.addMoreShortLinkIds([shortLinkId]);

const uuidGenerator = new UuidV4Generator();
const createNewEvent = makeCreateNewEvent({ uuidGenerator, timeGateway });
const saveNotificationAndRelatedEvent = makeSaveNotificationAndRelatedEvent(
createNewEvent,
uuidGenerator,
timeGateway,
);
Expand Down
Loading