-
Notifications
You must be signed in to change notification settings - Fork 11
/
actions.ts
83 lines (71 loc) · 2.93 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use server";
import { globalMailerService } from "@api/core-domain/infra/mail";
import { entrepriseService } from "@api/core-domain/infra/services";
import { referentRepo, representationEquilibreeRepo } from "@api/core-domain/repo";
import { GetRepresentationEquilibreeBySirenAndYear } from "@api/core-domain/useCases/GetRepresentationEquilibreeBySirenAndYear";
import { SaveRepresentationEquilibree } from "@api/core-domain/useCases/SaveRepresentationEquilibree";
import {
SendRepresentationEquilibreeReceipt,
SendRepresentationEquilibreeReceiptError,
} from "@api/core-domain/useCases/SendRepresentationEquilibreeReceipt";
import { jsxPdfService } from "@api/shared-domain/infra/pdf";
import { assertServerSession } from "@api/utils/auth";
import { type CreateRepresentationEquilibreeDTO } from "@common/core-domain/dtos/CreateRepresentationEquilibreeDTO";
export async function getRepresentationEquilibree(siren: string, year: number) {
await assertServerSession({
owner: {
check: siren,
message: "Not authorized to fetch repeq for this siren.",
},
staff: true,
});
// handle default errors
const useCase = new GetRepresentationEquilibreeBySirenAndYear(representationEquilibreeRepo);
const ret = await useCase.execute({ siren, year });
return ret;
}
export async function saveRepresentationEquilibree(repEq: CreateRepresentationEquilibreeDTO) {
const session = await assertServerSession({
owner: {
check: repEq.siren,
message: "Not authorized to save repeq for this siren.",
},
staff: true,
});
const useCase = new SaveRepresentationEquilibree(representationEquilibreeRepo, entrepriseService);
await useCase.execute({ repEq, override: session.user.staff });
const receiptUseCase = new SendRepresentationEquilibreeReceipt(
representationEquilibreeRepo,
referentRepo,
globalMailerService,
jsxPdfService,
);
await receiptUseCase.execute(repEq);
// Note: [revalidatePath bug](https://github.com/vercel/next.js/issues/49387). Try to reactivate it when it will be fixed in Next (it seems to be fixed in Next 14).
// revalidatePath(`/representation-equilibree/${repEq.siren}/${repEq.year}`);
// revalidatePath(`/representation-equilibree/${repEq.siren}/${repEq.year}/pdf`);
}
export async function sendRepresentationEquilibreeReceipt(siren: string, year: number) {
const session = await assertServerSession({
owner: {
check: siren,
message: "Not authorized to send repEq receipt for this siren.",
},
staff: true,
});
const useCase = new SendRepresentationEquilibreeReceipt(
representationEquilibreeRepo,
referentRepo,
globalMailerService,
jsxPdfService,
);
try {
await useCase.execute({ siren, year, email: session.user.email });
} catch (e: unknown) {
if (e instanceof SendRepresentationEquilibreeReceiptError) {
console.error(e.appErrorStack());
if (e.previousError) throw e.previousError;
}
throw e;
}
}