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

rework discussions #348

Merged
merged 7 commits into from
Jun 27, 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
43 changes: 35 additions & 8 deletions back/src/_testBuilders/DiscussionAggregateBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO : create a DiscussionAggregateBuilder
import { ApplicationCode } from "aws-sdk/clients/kinesisanalytics";
import { ImmersionObjective, SiretDto } from "shared";
import { AddressDto, Email, ImmersionObjective, SiretDto } from "shared";
import { DiscussionAggregate } from "../domain/immersionOffer/entities/DiscussionAggregate";

// TODO transform this function into a DiscussionAggregateBuilder
Expand All @@ -16,6 +16,8 @@ export const createDiscussionAggregate = ({
potentialBeneficiaryResumeLink,
potentialBeneficiaryPhone,
appellationCode,
establishmentContact = {},
address,
createdAt,
}: {
id: string;
Expand All @@ -24,16 +26,44 @@ export const createDiscussionAggregate = ({
immersionObjective: ImmersionObjective | null;
appellationCode: ApplicationCode;
potentialBeneficiaryResumeLink?: string;
address?: AddressDto;
establishmentContact?: Partial<{
email: Email;
firstName: string;
lastName: string;
phone: string;
job: string;
copyEmails: string[];
}>;
createdAt: Date;
}): DiscussionAggregate => ({
id,
appellationCode,
siret,
contactMode: "EMAIL",
createdAt,
potentialBeneficiaryFirstName: "Claire",
potentialBeneficiaryLastName: "Bertrand",
potentialBeneficiaryEmail: "claire.bertrand@email.fr",
immersionObjective,
address: address ?? {
streetNumberAndAddress: "1 rue de la Paix",
postcode: "75001",
departmentCode: "75",
city: "Paris",
},
potentialBeneficiary: {
firstName: "Claire",
lastName: "Bertrand",
email: "claire.bertrand@email.fr",
phone: potentialBeneficiaryPhone,
resumeLink: potentialBeneficiaryResumeLink,
},
establishmentContact: {
contactMode: "EMAIL",
email: establishmentContact.email ?? "establishment@mail.com",
firstName: establishmentContact.firstName ?? "Jean",
lastName: establishmentContact.lastName ?? "Dupont",
phone: establishmentContact.phone ?? "0123456789",
job: establishmentContact.job ?? "Directeur",
copyEmails: establishmentContact.copyEmails ?? [],
},
exchanges: [
{
sentAt: createdAt,
Expand All @@ -42,7 +72,4 @@ export const createDiscussionAggregate = ({
sender: "potentialBeneficiary",
},
],
immersionObjective,
potentialBeneficiaryPhone,
potentialBeneficiaryResumeLink,
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Pool, PoolClient } from "pg";
import { AppellationAndRomeDto } from "shared";
import { expectToEqual } from "shared";
import { createDiscussionAggregate } from "../../../_testBuilders/DiscussionAggregateBuilder";
import { EstablishmentAggregateBuilder } from "../../../_testBuilders/EstablishmentAggregateBuilder";
import { getTestPgPool } from "../../../_testBuilders/getTestPgPool";
Expand Down Expand Up @@ -70,6 +71,9 @@ describe("PgDiscussionAggregateRepository", () => {
immersionObjective,
potentialBeneficiaryPhone,
potentialBeneficiaryResumeLink,
establishmentContact: {
copyEmails: ["yolo@mail.com"],
},
appellationCode: styliste.appellationCode,
createdAt,
});
Expand All @@ -81,7 +85,7 @@ describe("PgDiscussionAggregateRepository", () => {
await pgDiscussionAggregateRepository.retrieveDiscussionAggregate(
discussionAggregate.id,
);
expect(retrievedDiscussionAggregate).toEqual(discussionAggregate);
expectToEqual(retrievedDiscussionAggregate, discussionAggregate);
});

it("Methode getDiscussionsBySiretSince", async () => {
Expand Down
74 changes: 55 additions & 19 deletions back/src/adapters/secondary/pg/PgDiscussionAggregateRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,40 @@ export class PgDiscussionAggregateRepository
potential_beneficiary_phone,
immersion_objective,
potential_beneficiary_resume_link,
created_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
created_at,
establishment_contact_email,
establishment_contact_first_name,
establishment_contact_last_name,
establishment_contact_phone,
establishment_contact_job,
establishment_contact_copy_emails,
street_number_and_address,
postcode,
department_code,
city
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)`,
[
discussion.id,
discussion.contactMode,
discussion.establishmentContact.contactMode,
discussion.siret,
discussion.appellationCode,
discussion.potentialBeneficiaryFirstName,
discussion.potentialBeneficiaryLastName,
discussion.potentialBeneficiaryEmail,
discussion.potentialBeneficiaryPhone,
discussion.potentialBeneficiary.firstName,
discussion.potentialBeneficiary.lastName,
discussion.potentialBeneficiary.email,
discussion.potentialBeneficiary.phone,
discussion.immersionObjective,
discussion.potentialBeneficiaryResumeLink,
discussion.potentialBeneficiary.resumeLink,
discussion.createdAt.toISOString(),
discussion.establishmentContact.email,
discussion.establishmentContact.firstName,
discussion.establishmentContact.lastName,
discussion.establishmentContact.phone,
discussion.establishmentContact.job,
JSON.stringify(discussion.establishmentContact.copyEmails),
discussion.address.streetNumberAndAddress,
discussion.address.postcode,
discussion.address.departmentCode,
discussion.address.city,
],
);
await Promise.all(
Expand Down Expand Up @@ -71,20 +91,36 @@ export class PgDiscussionAggregateRepository
GROUP BY discussion_id
)
SELECT
JSON_BUILD_OBJECT(
JSON_STRIP_NULLS(JSON_BUILD_OBJECT(
'id', id,
'contactMode', contact_mode,
'siret', siret,
'appellationCode', appellation_code::text,
'potentialBeneficiaryFirstName', potential_beneficiary_first_name,
'potentialBeneficiaryLastName', potential_beneficiary_last_name,
'potentialBeneficiaryEmail', potential_beneficiary_email,
'immersionObjective', immersion_objective ,
'potentialBeneficiaryPhone', potential_beneficiary_phone ,
'potentialBeneficiaryResumeLink', potential_beneficiary_resume_link ,
'createdAt', created_at,
'siret', siret,
'appellationCode', appellation_code::text,
'immersionObjective', immersion_objective,
'potentialBeneficiary', JSON_BUILD_OBJECT(
'firstName', potential_beneficiary_first_name,
'lastName', potential_beneficiary_last_name,
'email', potential_beneficiary_email,
'phone', potential_beneficiary_phone,
'resumeLink', potential_beneficiary_resume_link
),
'establishmentContact', JSON_BUILD_OBJECT(
'contactMode', contact_mode,
'firstName', establishment_contact_first_name,
'lastName', establishment_contact_last_name,
'email', establishment_contact_email,
'phone', establishment_contact_phone,
'job', establishment_contact_job,
'copyEmails', establishment_contact_copy_emails
),
'address', JSON_BUILD_OBJECT(
'streetNumberAndAddress', street_number_and_address,
'postcode', postcode,
'departmentCode', department_code,
'city', city
),
'exchanges', exchanges
) AS discussion
)) AS discussion
FROM discussions
LEFT JOIN exchanges_by_id ON exchanges_by_id.discussion_id = discussions.id
WHERE id = $1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { MigrationBuilder } from "node-pg-migrate";

const discussionsTable = "discussions";
const establishmentColumns = {
email: "establishment_contact_email",
firstName: "establishment_contact_first_name",
lastName: "establishment_contact_last_name",
phone: "establishment_contact_phone",
job: "establishment_contact_job",
copyEmails: "establishment_contact_copy_emails",
};
const addressColumns = {
streetNumberAndAddress: "street_number_and_address",
postcode: "postcode",
departmentCode: "department_code",
city: "city",
};

export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.addColumns(discussionsTable, {
[establishmentColumns.email]: { type: "text" },
[establishmentColumns.firstName]: { type: "text" },
[establishmentColumns.lastName]: { type: "text" },
[establishmentColumns.phone]: { type: "text", notNull: false }, // this is the only column that is nullable
[establishmentColumns.job]: { type: "text" },
[establishmentColumns.copyEmails]: { type: "jsonb" },
[addressColumns.streetNumberAndAddress]: { type: "text" },
[addressColumns.postcode]: { type: "text" },
[addressColumns.departmentCode]: { type: "text" },
[addressColumns.city]: { type: "text" },
});

pgm.sql(`
WITH data_to_update AS (
SELECT e.siret, c.email, c.firstname, c.lastname, c.phone, c.job, c.copy_emails, e.street_number_and_address, e.post_code, e.department_code, e.city
FROM establishments e
LEFT JOIN establishments__immersion_contacts eic ON eic.establishment_siret = e.siret
LEFT JOIN immersion_contacts c ON c.uuid = eic.contact_uuid
) UPDATE ${discussionsTable} d
SET
${establishmentColumns.email} = data_to_update.email,
${establishmentColumns.firstName} = data_to_update.firstname,
${establishmentColumns.lastName} = data_to_update.lastname,
${establishmentColumns.phone} = data_to_update.phone,
${establishmentColumns.job} = data_to_update.job,
${establishmentColumns.copyEmails} = data_to_update.copy_emails,
${addressColumns.streetNumberAndAddress} = data_to_update.street_number_and_address,
${addressColumns.postcode} = data_to_update.post_code,
${addressColumns.departmentCode} = data_to_update.department_code,
${addressColumns.city} = data_to_update.city
FROM data_to_update
WHERE d.siret = data_to_update.siret
`);

pgm.alterColumn(discussionsTable, establishmentColumns.email, {
notNull: true,
});
pgm.alterColumn(discussionsTable, establishmentColumns.firstName, {
notNull: true,
});
pgm.alterColumn(discussionsTable, establishmentColumns.lastName, {
notNull: true,
});
pgm.alterColumn(discussionsTable, establishmentColumns.phone, {
notNull: true,
});
pgm.alterColumn(discussionsTable, establishmentColumns.job, {
notNull: true,
});
pgm.alterColumn(discussionsTable, establishmentColumns.copyEmails, {
notNull: true,
});
pgm.alterColumn(discussionsTable, addressColumns.streetNumberAndAddress, {
notNull: true,
});
pgm.alterColumn(discussionsTable, addressColumns.postcode, {
notNull: true,
});
pgm.alterColumn(discussionsTable, addressColumns.departmentCode, {
notNull: true,
});
pgm.alterColumn(discussionsTable, addressColumns.city, {
notNull: true,
});
}

export async function down(pgm: MigrationBuilder): Promise<void> {
pgm.dropColumns(discussionsTable, [
establishmentColumns.email,
establishmentColumns.firstName,
establishmentColumns.lastName,
establishmentColumns.phone,
establishmentColumns.job,
establishmentColumns.copyEmails,
addressColumns.streetNumberAndAddress,
addressColumns.postcode,
addressColumns.departmentCode,
addressColumns.city,
]);
}
30 changes: 21 additions & 9 deletions back/src/domain/immersionOffer/entities/DiscussionAggregate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AddressDto,
AppellationCode,
ContactMethod,
Flavor,
Expand All @@ -11,17 +12,28 @@ export type DiscussionId = Flavor<string, "DiscussionId">;

export type DiscussionAggregate = {
id: DiscussionId;
potentialBeneficiaryFirstName: string;
potentialBeneficiaryLastName: string;
potentialBeneficiaryEmail: string;
potentialBeneficiaryPhone: string;
potentialBeneficiaryResumeLink?: string;
appellationCode: AppellationCode;
siret: string;
contactMode: ContactMethod;
createdAt: Date;
exchanges: ExchangeEntity[];
siret: string;
appellationCode: AppellationCode;
immersionObjective: ImmersionObjective | null;
address: AddressDto;
exchanges: ExchangeEntity[];
potentialBeneficiary: {
email: string;
firstName: string;
lastName: string;
phone: string;
resumeLink?: string;
};
establishmentContact: {
email: string;
copyEmails: string[];
firstName: string;
lastName: string;
phone: string;
job: string;
contactMode: ContactMethod;
};
};

export type ExchangeEntity = {
Expand Down
Loading