-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity.ts
More file actions
106 lines (95 loc) · 3.43 KB
/
Copy pathactivity.ts
File metadata and controls
106 lines (95 loc) · 3.43 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { randomUUID } from 'node:crypto';
import { Initiative } from '../utils/enums/application.ts';
import type { Repositories } from '../db/unitOfWork';
import type { Activity } from '../types/index.ts';
/**
* Create an activity for the given initiative with a unique identifier
* @param repositories - The required repositories
* @param initiative - The initiative code the activity will belong to
* @returns A Promise that resolves to the created activity
*/
export const createActivity = async (
repositories: Pick<Repositories, 'activity' | 'initiative'>,
initiative: Initiative
): Promise<Activity> => {
// Generate a new unique activity ID
let id, queryResult;
do {
id = randomUUID().substring(0, 8).toUpperCase();
queryResult = await repositories.activity.findUnique({
where: { activityId: id },
select: { activityId: true }
});
} while (queryResult);
// Create the activity
const initiativeResult = await repositories.initiative.findFirstOrThrow({ where: { code: initiative } });
const response = await repositories.activity.create({
activityId: id,
initiativeId: initiativeResult.initiativeId
});
return response;
};
/**
* Soft or hard delete an activity
* Soft delete will cascade soft deletes by manually calling each repository
* Hard delete utilizes set DB cascades
* @param repositories - The required repositories
* @param activityId - Unique activity ID
* @param options - Optional delete operation parameters
* @param options.hard - Force a hard delete of data in the database
*/
export const deleteActivity = async (
repositories: Pick<
Repositories,
| 'activity'
| 'activityContact'
| 'document'
| 'electrificationProject'
| 'enquiry'
| 'generalProject'
| 'housingProject'
| 'note'
| 'noteHistory'
| 'permit'
| 'permitNote'
| 'permitTracking'
>,
activityId: string,
options?: { hard?: boolean }
): Promise<void> => {
await repositories.activity.delete({ activityId, deletedAt: null }, options);
if (!options?.hard) {
await repositories.activityContact.deleteMany({ activityId, deletedAt: null });
await repositories.document.deleteMany({ activityId, deletedAt: null });
await repositories.electrificationProject.deleteMany({ activityId, deletedAt: null });
await repositories.enquiry.deleteMany({ activityId, deletedAt: null });
await repositories.generalProject.deleteMany({ activityId, deletedAt: null });
await repositories.housingProject.deleteMany({ activityId, deletedAt: null });
await repositories.noteHistory.deleteMany({ activityId, deletedAt: null });
await repositories.permit.deleteMany({ activityId, deletedAt: null });
const deletedNotes = (
await repositories.noteHistory.findMany(
{ where: { activityId }, select: { noteHistoryId: true } },
{ includeDeleted: true }
)
).map((x) => x.noteHistoryId);
const deletedPermits = (
await repositories.permit.findMany(
{ where: { activityId }, select: { permitId: true } },
{ includeDeleted: true }
)
).map((x) => x.permitId);
await repositories.note.deleteMany({
noteHistoryId: { in: deletedNotes },
deletedAt: null
});
await repositories.permitNote.deleteMany({
permitId: { in: deletedPermits },
deletedAt: null
});
await repositories.permitTracking.deleteMany({
permitId: { in: deletedPermits },
deletedAt: null
});
}
};