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

#2776 #2778 - envoyer les mails de relances du bilan #2835

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
1 change: 1 addition & 0 deletions back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"trigger-delete-old-discussion-messages": "ts-node src/scripts/triggerDeleteOldDiscussionMessages",
"trigger-refresh-materialized-views": "ts-node src/scripts/triggerRefreshMaterializedViews.ts",
"trigger-resync-old-conventions-to-pe": "ts-node src/scripts/triggerResyncOldConventionsToPe.ts",
"trigger-assessment-reminders": "ts-node src/scripts/triggerAssessmentReminder.ts",
"trigger-sending-emails-with-assessment-creation-link": "ts-node src/scripts/triggerSendingEmailsWithAssessmentCreationLink.ts",
"trigger-sending-beneficiary-assessment-emails": "ts-node src/scripts/triggerSendingBeneficiaryPdfAssessmentEmails.ts",
"trigger-suggest-edit-form-establishment-every-6-months": "ts-node src/scripts/triggerSuggestEditFormEstablishmentEvery6Months.ts",
Expand Down
4 changes: 4 additions & 0 deletions back/scalingo/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"command": "0 21 * * * pnpm back run trigger-delete-email-attachements",
"size": "M"
},
{
"command": "15 21 * * * pnpm back run trigger-assessment-reminders",
"size": "M"
},
{
"command": "0 22 * * * pnpm back run trigger-mark-old-convention-as-deprecated",
"size": "M"
Expand Down
9 changes: 9 additions & 0 deletions back/src/config/bootstrap/createUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import { UuidGenerator } from "../../domains/core/uuid-generator/ports/UuidGener
import { AddEstablishmentLead } from "../../domains/establishment/use-cases/AddEstablishmentLead";
import { AddFormEstablishment } from "../../domains/establishment/use-cases/AddFormEstablishment";
import { AddFormEstablishmentBatch } from "../../domains/establishment/use-cases/AddFormEstablismentsBatch";
import { makeAssessmentReminder } from "../../domains/establishment/use-cases/AssessmentReminder";
import { ContactEstablishment } from "../../domains/establishment/use-cases/ContactEstablishment";
import { makeContactRequestReminder } from "../../domains/establishment/use-cases/ContactRequestReminder";
import { DeleteEstablishment } from "../../domains/establishment/use-cases/DeleteEstablishment";
Expand Down Expand Up @@ -633,6 +634,14 @@ export const createUseCases = (
})),
}),

assessmentReminder: makeAssessmentReminder({
uowPerformer,
deps: {
timeGateway: gateways.timeGateway,
saveNotificationAndRelatedEvent,
generateConventionMagicLinkUrl,
},
}),
getAgencyById: makeGetAgencyById({
uowPerformer,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export class InMemoryAssessmentRepository implements AssessmentRepository {
);
}

public async getByConventionIds(
conventionIds: ConventionId[],
): Promise<AssessmentEntity[]> {
return this.#assessments.filter((assessment) =>
conventionIds.includes(assessment.conventionId),
);
}

public async save(assessment: AssessmentEntity): Promise<void> {
this.#assessments.push(assessment);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { addDays } from "date-fns";
import subDays from "date-fns/subDays";
import { values } from "ramda";
import {
ConventionDto,
ConventionId,
ConventionReadDto,
Email,
errors,
validatedConventionStatuses,
} from "shared";
import { ConventionRepository } from "../ports/ConventionRepository";

Expand All @@ -19,6 +22,19 @@ export class InMemoryConventionRepository implements ConventionRepository {
throw new Error("not implemented");
}

public async getIdsValidatedByEndDateAround(
dateEnd: Date,
): Promise<ConventionId[]> {
return values(this.#conventions)
.filter(
(convention) =>
validatedConventionStatuses.includes(convention.status) &&
new Date(convention.dateEnd) >= subDays(dateEnd, 1) &&
new Date(convention.dateEnd) <= addDays(dateEnd, 1),
)
.map((convention) => convention.id);
}

public async getById(id: ConventionId) {
return this.#conventions[id];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,17 @@ describe("PgAssessmentRepository", () => {
);
});
});

describe("getByConventionIds", () => {
it("returns assessment found", async () => {
await assessmentRepository.save(minimalAssessment);

expectToEqual(
await assessmentRepository.getByConventionIds([
minimalAssessment.conventionId,
]),
[minimalAssessment],
);
});
});
});
91 changes: 57 additions & 34 deletions back/src/domains/convention/adapters/PgAssessmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,82 @@ import {
import { AssessmentEntity } from "../entities/AssessmentEntity";
import { AssessmentRepository } from "../ports/AssessmentRepository";

const createAssessmentQueryBuilder = (transaction: KyselyDb) => {
return transaction.selectFrom("immersion_assessments").select((eb) => [
jsonBuildObject({
conventionId: eb.ref("convention_id"),
status: eb.ref("status").$castTo<AssessmentStatus[]>(),
establishmentFeedback: eb.ref("establishment_feedback"),
establishmentAdvices: eb.ref("establishment_advices"),
endedWithAJob: eb.ref("ended_with_a_job"),
contractStartDate: sql<DateString>`date_to_iso(contract_start_date)`,
typeOfContract: eb.ref("type_of_contract"),
lastDayOfPresence: sql<DateString>`date_to_iso(last_day_of_presence)`,
numberOfMissedHours: eb.ref("number_of_missed_hours"),
}).as("assessment"),
]);
};

const parseAssessmentSchema = (assessment: any) => {
return assessmentSchema.parse({
conventionId: assessment.conventionId,
status: assessment.status,
establishmentFeedback: assessment.establishmentFeedback,
establishmentAdvices: assessment.establishmentAdvices,
endedWithAJob: assessment.endedWithAJob,
...(assessment.contractStartDate
? { contractStartDate: assessment.contractStartDate }
: {}),
...(assessment.typeOfContract
? { typeOfContract: assessment.typeOfContract }
: {}),
...(assessment.lastDayOfPresence
? { lastDayOfPresence: assessment.lastDayOfPresence }
: {}),
...(assessment.numberOfMissedHours !== null
? { numberOfMissedHours: assessment.numberOfMissedHours }
: {}),
});
};

export class PgAssessmentRepository implements AssessmentRepository {
constructor(private transaction: KyselyDb) {}

public async getByConventionId(
conventionId: ConventionId,
): Promise<AssessmentEntity | undefined> {
const result = await this.transaction
.selectFrom("immersion_assessments")
.select((eb) => [
jsonBuildObject({
conventionId: eb.ref("convention_id"),
status: eb.ref("status").$castTo<AssessmentStatus[]>(),
establishmentFeedback: eb.ref("establishment_feedback"),
establishmentAdvices: eb.ref("establishment_advices"),
endedWithAJob: eb.ref("ended_with_a_job"),
contractStartDate: sql<DateString>`date_to_iso(contract_start_date)`,
typeOfContract: eb.ref("type_of_contract"),
lastDayOfPresence: sql<DateString>`date_to_iso(last_day_of_presence)`,
numberOfMissedHours: eb.ref("number_of_missed_hours"),
}).as("assessment"),
])
const result = await createAssessmentQueryBuilder(this.transaction)
.where("convention_id", "=", conventionId)
.executeTakeFirst();

const assessment = result?.assessment;
if (!assessment) return;

const dto = assessmentSchema.parse({
conventionId: assessment.conventionId,
status: assessment.status,
establishmentFeedback: assessment.establishmentFeedback,
establishmentAdvices: assessment.establishmentAdvices,
endedWithAJob: assessment.endedWithAJob,
...(assessment.contractStartDate
? { contractStartDate: assessment.contractStartDate }
: {}),
...(assessment.typeOfContract
? { typeOfContract: assessment.typeOfContract }
: {}),
...(assessment.lastDayOfPresence
? { lastDayOfPresence: assessment.lastDayOfPresence }
: {}),
...(assessment.numberOfMissedHours !== null
? { numberOfMissedHours: assessment.numberOfMissedHours }
: {}),
});
const dto = parseAssessmentSchema(assessment);

return {
_entityName: "Assessment",
...dto,
};
}

public async getByConventionIds(
conventionIds: ConventionId[],
): Promise<AssessmentEntity[]> {
if (conventionIds.length === 0) return [];

const result = await createAssessmentQueryBuilder(this.transaction)
.where("convention_id", "in", conventionIds)
.execute();

return result.map(({ assessment }) => {
return {
_entityName: "Assessment",
...parseAssessmentSchema(assessment),
};
});
}

public async save(assessmentEntity: AssessmentEntity): Promise<void> {
await this.transaction
.insertInto("immersion_assessments")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { KyselyDb, makeKyselyDb } from "../../../config/pg/kysely/kyselyUtils";
import { getTestPgPool } from "../../../config/pg/pgUtils";
import { toAgencyWithRights } from "../../../utils/agency";
import { PgAgencyRepository } from "../../agency/adapters/PgAgencyRepository";
import { CustomTimeGateway } from "../../core/time-gateway/adapters/CustomTimeGateway";
import { PgConventionRepository } from "./PgConventionRepository";

describe("PgConventionRepository", () => {
Expand All @@ -47,13 +48,15 @@ describe("PgConventionRepository", () => {
let pool: Pool;
let conventionRepository: PgConventionRepository;
let db: KyselyDb;
let timeGateway: CustomTimeGateway;

beforeAll(async () => {
pool = getTestPgPool();
db = makeKyselyDb(pool);
await new PgAgencyRepository(db).insert(
toAgencyWithRights(AgencyDtoBuilder.create().build()),
);
timeGateway = new CustomTimeGateway();
});

afterAll(async () => {
Expand Down Expand Up @@ -952,6 +955,36 @@ describe("PgConventionRepository", () => {
});
});

describe("getIdsValidatedByEndDateAround", () => {
it("retrieve validated convention ids when endDate match", async () => {
const now = timeGateway.now();
const convention = new ConventionDtoBuilder()
.withStatus("ACCEPTED_BY_VALIDATOR")
.withDateEnd(now.toISOString())
.build();
await conventionRepository.save(convention);

const result =
await conventionRepository.getIdsValidatedByEndDateAround(now);

expectToEqual(result, [convention.id]);
});

it("retrieve nothing when endDate does not match", async () => {
const now = timeGateway.now();
const convention = new ConventionDtoBuilder()
.withDateEnd(now.toISOString())
.build();
await conventionRepository.save(convention);

const result = await conventionRepository.getIdsValidatedByEndDateAround(
addDays(now, 2),
);

expectToEqual(result, []);
});
});

describe("getIdsByEstablishmentTutorEmail", () => {
it("retrieve convention id when tutor email match", async () => {
const email = "[email protected]";
Expand Down
23 changes: 23 additions & 0 deletions back/src/domains/convention/adapters/PgConventionRepository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { addDays } from "date-fns";
import subDays from "date-fns/subDays";
import { sql } from "kysely";
import {
Beneficiary,
Expand All @@ -12,6 +14,7 @@ import {
errors,
isBeneficiaryStudent,
isEstablishmentTutorIsEstablishmentRepresentative,
validatedConventionStatuses,
} from "shared";
import { KyselyDb, falsyToNull } from "../../../config/pg/kysely/kyselyUtils";
import { ConventionRepository } from "../ports/ConventionRepository";
Expand Down Expand Up @@ -88,6 +91,26 @@ export class PgConventionRepository implements ConventionRepository {
return result.map(({ id }) => id);
}

public async getIdsValidatedByEndDateAround(endDate: Date) {
const result = await this.transaction
.selectFrom("conventions")
.select("conventions.id")
.where("conventions.status", "in", validatedConventionStatuses)
.where(
sql`conventions.date_end`,
">=",
subDays(endDate, 1).toISOString().split("T")[0],
)
.where(
sql`conventions.date_end`,
"<=",
addDays(endDate, 1).toISOString().split("T")[0],
)
.execute();

return result.map(({ id }) => id);
}

public async save(convention: ConventionDto): Promise<void> {
const alreadyExistingConvention = await this.getById(convention.id);
if (alreadyExistingConvention)
Expand Down
3 changes: 3 additions & 0 deletions back/src/domains/convention/ports/AssessmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ export interface AssessmentRepository {
getByConventionId(
conventionId: ConventionId,
): Promise<AssessmentEntity | undefined>;
getByConventionIds(
conventionIds: ConventionId[],
): Promise<AssessmentEntity[]>;
}
1 change: 1 addition & 0 deletions back/src/domains/convention/ports/ConventionRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface ConventionRepository {
email: Email,
): Promise<ConventionId[]>;
getIdsByEstablishmentTutorEmail(email: Email): Promise<ConventionId[]>;
getIdsValidatedByEndDateAround: (endDate: Date) => Promise<ConventionId[]>;
save: (conventionDto: ConventionDto) => Promise<void>;
getById: (id: ConventionId) => Promise<ConventionDto | undefined>;
update: (conventionDto: ConventionDto) => Promise<ConventionId | undefined>;
Expand Down
Loading
Loading