@tekkare/romulus 0.1.261 → 0.1.262
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/layer/app/components/panels/OipPanelProjects.vue +70 -21
- package/layer/app/components/project/AddButton.vue +127 -0
- package/layer/app/components/project/Banner.vue +68 -0
- package/layer/app/components/project/Dashboard.vue +85 -0
- package/layer/app/components/project/DiscardConfirm.vue +74 -0
- package/layer/app/components/project/Documents.vue +611 -0
- package/layer/app/components/project/Export.vue +205 -0
- package/layer/app/components/project/Inbox.vue +1241 -0
- package/layer/app/components/project/ListPage.vue +102 -0
- package/layer/app/components/project/NewButton.vue +147 -0
- package/layer/app/components/project/Notes.vue +1158 -0
- package/layer/app/components/project/Page.vue +820 -0
- package/layer/app/components/project/Share.vue +135 -0
- package/layer/app/components/project/Watch.vue +273 -0
- package/layer/app/composables/useOipProjectKinds.ts +71 -0
- package/layer/app/composables/useOipResourceHooks.ts +8 -3
- package/layer/app/composables/useProjectDashboard.ts +22 -0
- package/layer/app/composables/useProjects.ts +646 -0
- package/layer/app/oip-defaults/projects.ts +175 -0
- package/layer/app/pages/projects/[id].vue +8 -0
- package/layer/app/pages/projects/index.vue +8 -0
- package/layer/app/types/projects.ts +168 -0
- package/layer/app/utils/projectNoteWelcome.ts +137 -0
- package/layer/nuxt.config.ts +16 -0
- package/layer/server/api/oip/sharing/artifacts.get.ts +44 -0
- package/package.json +2 -1
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Les projets : un dossier partage, tenu par l'API Virtual Lab, ou
|
|
3
|
+
* l'utilisateur range ce qu'il croise au fil des pages pour le retrouver
|
|
4
|
+
* ensemble. Servi par le layer OIP a toute app qui l'etend (repris d'OIP Case
|
|
5
|
+
* Atlas, France Augmented Science et fr_drugs_app) : l'affichage vient des
|
|
6
|
+
* composants RmProject* de Romulus, ce composable tient l'etat et les appels
|
|
7
|
+
* (client `useOipApi` de l'app).
|
|
8
|
+
*
|
|
9
|
+
* Le projet actif est un choix d'application, pas de page : il tient d'un
|
|
10
|
+
* ecran a l'autre (localStorage) et se laisse imposer par l'URL
|
|
11
|
+
* (`?project=<uuid>`), ce qui est la facon dont Virtual Lab renvoie ici sur
|
|
12
|
+
* un projet precis.
|
|
13
|
+
*
|
|
14
|
+
* L'API ne garde d'un element que sa paire (resourceId, resourceType) : c'est
|
|
15
|
+
* useOipProjectKinds qui sait la relire, pas ce composable.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { RmProject, RmProjectArchivedEntry } from "@tekkare/romulus/utils/projects";
|
|
19
|
+
import { useOipApi } from "#oip-app/api";
|
|
20
|
+
import { oipSnackbar } from "#oip-app/ui";
|
|
21
|
+
import { PROJECTS_PATH, PROJECTS_STORAGE_KEY } from "#oip-projects";
|
|
22
|
+
import { useOipProjectKinds } from "./useOipProjectKinds";
|
|
23
|
+
|
|
24
|
+
export interface Project {
|
|
25
|
+
id: string;
|
|
26
|
+
title: string;
|
|
27
|
+
description: string;
|
|
28
|
+
createdAt?: string;
|
|
29
|
+
owner?: { userId: string; email: string; displayName: string | null };
|
|
30
|
+
/** Ouvert a tout l'espace de travail. */
|
|
31
|
+
isShared?: boolean;
|
|
32
|
+
/** Utilisateurs qui peuvent l'ouvrir, proprietaire compris. */
|
|
33
|
+
membersCount?: number;
|
|
34
|
+
/** Ressources presentes, tous types confondus, y compris ceux que l'app n'affiche pas. */
|
|
35
|
+
resourcesCount?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Un document, ou les documents que porte une meme carte. */
|
|
39
|
+
export type ProjectResourceId = string | number | Array<string | number>;
|
|
40
|
+
|
|
41
|
+
export interface ProjectItem {
|
|
42
|
+
resourceId: string;
|
|
43
|
+
resourceType: string;
|
|
44
|
+
createdAt?: string;
|
|
45
|
+
/** Commentaires et citations dans les notes, comptes par l'API avec la liste. */
|
|
46
|
+
commentsCount?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Une carte a l'ecran : ses documents, leur famille, et de quoi la nommer. */
|
|
50
|
+
export interface ProjectAddable {
|
|
51
|
+
ids: string[];
|
|
52
|
+
resourceType: string;
|
|
53
|
+
label: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Lues a l'appel : le contrat de l'app peut importer des composants qui
|
|
57
|
+
// importent ce composable, un cycle ou la constante n'existe pas encore.
|
|
58
|
+
const storageKey = () => `${PROJECTS_STORAGE_KEY}Id`;
|
|
59
|
+
/**
|
|
60
|
+
* Les projets epingles. Un choix de lecture, pas de contenu : l'API ne
|
|
61
|
+
* connait pas d'epingle, elle reste donc dans ce navigateur.
|
|
62
|
+
*/
|
|
63
|
+
const pinsKey = () => `${PROJECTS_STORAGE_KEY}Pins`;
|
|
64
|
+
|
|
65
|
+
/** Une seule requete de liste en vol, quel que soit le nombre d'appelants. */
|
|
66
|
+
let listInflight: Promise<void> | null = null;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Compteur des changements locaux des elements (projet actif, ajout,
|
|
70
|
+
* retrait). Une liste rendue par l'API apres l'un d'eux est perimee : sans
|
|
71
|
+
* lui, un GET parti avant un ajout ecrasait l'element tout juste ajoute.
|
|
72
|
+
*/
|
|
73
|
+
let itemsVersion = 0;
|
|
74
|
+
|
|
75
|
+
export const useProjects = () => {
|
|
76
|
+
const projects = useState<Project[]>("projects", () => []);
|
|
77
|
+
const loaded = useState<boolean>("projects-loaded", () => false);
|
|
78
|
+
const pending = useState<boolean>("projects-pending", () => false);
|
|
79
|
+
/** Renseigne quand l'API refuse ou n'est pas joignable : evite la relance en boucle. */
|
|
80
|
+
const failure = useState<string | null>("projects-failure", () => null);
|
|
81
|
+
|
|
82
|
+
const activeId = useState<string | null>("project-active", () => null);
|
|
83
|
+
const pinnedIds = useState<string[]>("project-pins", () => []);
|
|
84
|
+
/** Les elements du projet actif, tels que l'API les rend. */
|
|
85
|
+
const items = useState<ProjectItem[]>("project-items", () => []);
|
|
86
|
+
const itemsPending = useState<boolean>("project-items-pending", () => false);
|
|
87
|
+
/** Projet dont les elements sont charges : un projet vide a une liste vide. */
|
|
88
|
+
const itemsLoadedFor = useState<string | null>("project-items-loaded", () => null);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Ce que l'ecran courant sait ranger. Les boutons s'inscrivent ici au
|
|
92
|
+
* montage et s'effacent au demontage : ce que le bandeau propose d'ajouter
|
|
93
|
+
* est exactement ce que la page affiche, y compris apres un filtre.
|
|
94
|
+
*/
|
|
95
|
+
const onPage = useState<Record<string, ProjectAddable>>("project-onpage", () => ({}));
|
|
96
|
+
|
|
97
|
+
const { status, data: session } = useAuth();
|
|
98
|
+
const route = useRoute();
|
|
99
|
+
const { locale } = useI18n();
|
|
100
|
+
const localePath = useLocalePath();
|
|
101
|
+
const en = computed(() => locale.value === "en");
|
|
102
|
+
const lang = computed<"fr" | "en">(() => (en.value ? "en" : "fr"));
|
|
103
|
+
|
|
104
|
+
const api = () => useOipApi();
|
|
105
|
+
/** Lu une fois, dans le contexte de l'app : les appels partent plus tard. */
|
|
106
|
+
const knownTypes = new Set<string>(useOipProjectKinds().map((kind) => kind.type));
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Le retour de l'action. Ranger une fiche ne change rien de visible en
|
|
110
|
+
* dehors du bouton : sans un mot, on ne sait pas si le clic est parti, ni
|
|
111
|
+
* ou. L'echec en dit la raison.
|
|
112
|
+
*/
|
|
113
|
+
function notify(text: string, ok = true) {
|
|
114
|
+
oipSnackbar([
|
|
115
|
+
{ attribute: "isVisible", value: true },
|
|
116
|
+
{ attribute: "text", value: text },
|
|
117
|
+
{ attribute: "loader", value: false },
|
|
118
|
+
{ attribute: "prefixIcon", value: ok ? "Check" : "WarningCircle" },
|
|
119
|
+
{ attribute: "prefixIconColor", value: ok ? "var(--color-success)" : "var(--color-danger)" },
|
|
120
|
+
{ attribute: "hasTimeOut", value: true },
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function reasonOf(error: any) {
|
|
125
|
+
return error?.data?.error || error?.data?.message || error?.message || "";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function addFailure(error: any) {
|
|
129
|
+
const reason = reasonOf(error);
|
|
130
|
+
return reason ? `${en.value ? "Not added" : "Non ajouté"} : ${reason}` : en.value ? "Not added" : "Non ajouté";
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Les fiches refusees parce qu'archivees : un retrait archive la ligne,
|
|
135
|
+
* l'ajout la refuse ensuite. Ce n'est pas un echec mais une question a
|
|
136
|
+
* poser (« la desarchiver ? »). Le repli sur `notFound` sert tant que l'API
|
|
137
|
+
* ne rend pas `archived`.
|
|
138
|
+
*/
|
|
139
|
+
function archivedOf(response: any): { resourceId: string; resourceType: string }[] {
|
|
140
|
+
const explicit = Array.isArray(response?.archived) ? response.archived : null;
|
|
141
|
+
const source =
|
|
142
|
+
explicit ?? (response?.notFound ?? []).filter((entry: any) => /archiv/i.test(entry?.esError ?? ""));
|
|
143
|
+
return source.map((entry: any) => ({
|
|
144
|
+
resourceId: String(entry.resourceId),
|
|
145
|
+
resourceType: String(entry.resourceType),
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function nameOf(projectId: string | null) {
|
|
150
|
+
return projects.value.find((p) => p.id === projectId)?.title ?? "";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const active = computed<Project | null>(() => projects.value.find((p) => p.id === activeId.value) ?? null);
|
|
154
|
+
|
|
155
|
+
const myEmail = computed(() => String((session.value as any)?.user?.email ?? "").toLowerCase());
|
|
156
|
+
|
|
157
|
+
function isMine(project: Pick<Project, "owner">) {
|
|
158
|
+
const owner = String(project.owner?.email ?? "").toLowerCase();
|
|
159
|
+
return !!owner && owner === myEmail.value;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Partage s'il est ouvert a l'espace ou si quelqu'un d'autre y est. */
|
|
163
|
+
function isShared(project: Project) {
|
|
164
|
+
return !!project.isShared || (project.membersCount ?? 1) > 1;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function pathOf(projectId: string) {
|
|
168
|
+
return localePath(`${PROJECTS_PATH}/${projectId}`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Le projet tel que les composants RmProject* l'affichent. */
|
|
172
|
+
function toRm(project: Project): RmProject {
|
|
173
|
+
return {
|
|
174
|
+
id: project.id,
|
|
175
|
+
title: project.title,
|
|
176
|
+
description: project.description,
|
|
177
|
+
createdAt: project.createdAt,
|
|
178
|
+
ownerName: project.owner?.displayName || project.owner?.email || "",
|
|
179
|
+
mine: isMine(project),
|
|
180
|
+
shared: isShared(project),
|
|
181
|
+
resourcesCount: project.resourcesCount,
|
|
182
|
+
pinned: pinnedIds.value.includes(project.id),
|
|
183
|
+
to: pathOf(project.id),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const rmProjects = computed(() => projects.value.map(toRm));
|
|
188
|
+
|
|
189
|
+
// ------------------------------------------------------------ les epingles
|
|
190
|
+
|
|
191
|
+
function restorePins() {
|
|
192
|
+
if (import.meta.server) return;
|
|
193
|
+
try {
|
|
194
|
+
const raw = JSON.parse(localStorage.getItem(pinsKey()) ?? "[]");
|
|
195
|
+
pinnedIds.value = Array.isArray(raw) ? raw.map(String) : [];
|
|
196
|
+
} catch {
|
|
197
|
+
pinnedIds.value = [];
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function togglePin(projectId: string) {
|
|
202
|
+
pinnedIds.value = pinnedIds.value.includes(projectId)
|
|
203
|
+
? pinnedIds.value.filter((id) => id !== projectId)
|
|
204
|
+
: [...pinnedIds.value, projectId];
|
|
205
|
+
if (import.meta.client) localStorage.setItem(pinsKey(), JSON.stringify(pinnedIds.value));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ------------------------------------------------------------- les projets
|
|
209
|
+
|
|
210
|
+
async function fetchList() {
|
|
211
|
+
pending.value = true;
|
|
212
|
+
try {
|
|
213
|
+
const response = await api().projects.list();
|
|
214
|
+
projects.value = (response?.projects ?? []).map((p: any) => ({
|
|
215
|
+
id: p.projectId,
|
|
216
|
+
title: p.title,
|
|
217
|
+
description: p.description,
|
|
218
|
+
createdAt: p.createdAt,
|
|
219
|
+
owner: p.owner,
|
|
220
|
+
isShared: p.isShared,
|
|
221
|
+
membersCount: p.membersCount,
|
|
222
|
+
resourcesCount: p.resourcesCount,
|
|
223
|
+
}));
|
|
224
|
+
failure.value = null;
|
|
225
|
+
loaded.value = true;
|
|
226
|
+
} catch (error: any) {
|
|
227
|
+
failure.value = reasonOf(error) || "unavailable";
|
|
228
|
+
projects.value = [];
|
|
229
|
+
} finally {
|
|
230
|
+
pending.value = false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function load(force = false) {
|
|
235
|
+
// Client seulement : l'API s'identifie par le jeton de la session.
|
|
236
|
+
if (import.meta.server) return;
|
|
237
|
+
if (status.value !== "authenticated") return;
|
|
238
|
+
if (listInflight) {
|
|
239
|
+
await listInflight;
|
|
240
|
+
if (!force) return;
|
|
241
|
+
}
|
|
242
|
+
if ((loaded.value || failure.value) && !force) return;
|
|
243
|
+
listInflight = fetchList().finally(() => {
|
|
244
|
+
listInflight = null;
|
|
245
|
+
});
|
|
246
|
+
await listInflight;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** `scope` : le cadrage structure pose par une creation assistee (`projects.scope` cote API). */
|
|
250
|
+
async function create(title: string, description = "", scope?: object) {
|
|
251
|
+
const created = await api().projects.create({ title, description, ...(scope ? { scope } : {}) });
|
|
252
|
+
const project: Project = {
|
|
253
|
+
id: created.projectId,
|
|
254
|
+
title: created.title,
|
|
255
|
+
description: created.description,
|
|
256
|
+
createdAt: created.createdAt,
|
|
257
|
+
owner: created.owner,
|
|
258
|
+
isShared: created.isShared,
|
|
259
|
+
membersCount: 1,
|
|
260
|
+
resourcesCount: 0,
|
|
261
|
+
};
|
|
262
|
+
projects.value = [project, ...projects.value];
|
|
263
|
+
return project;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Met a jour un projet. La liste locale est corrigee avec la reponse de
|
|
268
|
+
* l'API : le rail, le bandeau et la page lisent tous le meme tableau.
|
|
269
|
+
*/
|
|
270
|
+
async function update(projectId: string, payload: { title?: string; description?: string; isShared?: boolean }) {
|
|
271
|
+
const updated = await api().projects.update(projectId, payload);
|
|
272
|
+
projects.value = projects.value.map((project) =>
|
|
273
|
+
project.id === projectId
|
|
274
|
+
? {
|
|
275
|
+
...project,
|
|
276
|
+
title: updated?.title ?? payload.title ?? project.title,
|
|
277
|
+
description: updated?.description ?? payload.description ?? project.description,
|
|
278
|
+
isShared: updated?.isShared ?? payload.isShared ?? project.isShared,
|
|
279
|
+
}
|
|
280
|
+
: project,
|
|
281
|
+
);
|
|
282
|
+
return updated;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Supprime un projet pour de bon : l'API emporte ses ressources et ses
|
|
287
|
+
* commentaires. Reserve au proprietaire (403 sinon).
|
|
288
|
+
*/
|
|
289
|
+
async function destroy(projectId: string) {
|
|
290
|
+
await api().projects.delete(projectId);
|
|
291
|
+
projects.value = projects.value.filter((project) => project.id !== projectId);
|
|
292
|
+
if (activeId.value === projectId) setActive(null);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// --------------------------------------------------------- le projet actif
|
|
296
|
+
|
|
297
|
+
/** Restaure le projet actif (l'URL d'abord, le dernier choix ensuite) et les epingles. */
|
|
298
|
+
function restoreActive() {
|
|
299
|
+
if (import.meta.server) return;
|
|
300
|
+
restorePins();
|
|
301
|
+
const fromUrl = route.query.project;
|
|
302
|
+
const id = typeof fromUrl === "string" && fromUrl ? fromUrl : localStorage.getItem(storageKey());
|
|
303
|
+
if (id && id !== activeId.value) activate(id);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function setActive(id: string | null) {
|
|
307
|
+
if (import.meta.client) {
|
|
308
|
+
if (id) localStorage.setItem(storageKey(), id);
|
|
309
|
+
else localStorage.removeItem(storageKey());
|
|
310
|
+
}
|
|
311
|
+
activate(id);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Change le projet actif et vide les elements de l'ancien. */
|
|
315
|
+
function activate(id: string | null) {
|
|
316
|
+
activeId.value = id;
|
|
317
|
+
items.value = [];
|
|
318
|
+
itemsLoadedFor.value = null;
|
|
319
|
+
itemsVersion++;
|
|
320
|
+
if (id) loadItems();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ------------------------------------------------------------ les elements
|
|
324
|
+
|
|
325
|
+
async function loadItems(force = false) {
|
|
326
|
+
const id = activeId.value;
|
|
327
|
+
if (!id || import.meta.server) return;
|
|
328
|
+
if (itemsLoadedFor.value === id && !force) return;
|
|
329
|
+
if (itemsPending.value && !force) return;
|
|
330
|
+
itemsPending.value = true;
|
|
331
|
+
const version = itemsVersion;
|
|
332
|
+
try {
|
|
333
|
+
const fetched = await fetchItems(id);
|
|
334
|
+
// Un ajout, un retrait ou un changement de projet a eu lieu pendant la
|
|
335
|
+
// requete : cette liste est deja perimee, on relit.
|
|
336
|
+
if (version !== itemsVersion) return await loadItems(true);
|
|
337
|
+
items.value = fetched;
|
|
338
|
+
itemsLoadedFor.value = id;
|
|
339
|
+
} catch (error: any) {
|
|
340
|
+
if (version === itemsVersion) items.value = [];
|
|
341
|
+
// Projet retenu (localStorage, URL) supprime ou d'un autre espace : on
|
|
342
|
+
// l'oublie, sinon chaque ecran relance la meme requete en 404.
|
|
343
|
+
if ((error?.response?.status ?? error?.statusCode) === 404 && activeId.value === id) setActive(null);
|
|
344
|
+
} finally {
|
|
345
|
+
itemsPending.value = false;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Les elements d'un projet quelconque. Passent les familles que le layer
|
|
351
|
+
* decrit (cf. useOipProjectKinds) : un type que personne ne sait relire
|
|
352
|
+
* reste dans le projet, cache ici, et ne compte pas dans les totaux.
|
|
353
|
+
*/
|
|
354
|
+
async function fetchItems(projectId: string): Promise<ProjectItem[]> {
|
|
355
|
+
const response = await api().projectResources.list(projectId);
|
|
356
|
+
return (response?.data ?? [])
|
|
357
|
+
.filter((item: any) => knownTypes.has(String(item.resourceType)))
|
|
358
|
+
.map((item: any) => ({
|
|
359
|
+
resourceId: String(item.resourceId),
|
|
360
|
+
resourceType: String(item.resourceType),
|
|
361
|
+
createdAt: item.createdAt,
|
|
362
|
+
commentsCount: Number(item.commentsCount ?? 0) + Number(item.noteMentionsCount ?? 0),
|
|
363
|
+
}));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Une carte est « dans le projet » quand TOUS ses documents y sont. */
|
|
367
|
+
function idList(resourceId: ProjectResourceId): string[] {
|
|
368
|
+
const raw = Array.isArray(resourceId) ? resourceId : [resourceId];
|
|
369
|
+
return [...new Set(raw.filter((value) => value != null && value !== "").map(String))];
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function has(resourceId: ProjectResourceId, resourceType: string) {
|
|
373
|
+
const ids = idList(resourceId);
|
|
374
|
+
if (!ids.length) return false;
|
|
375
|
+
return ids.every((id) => items.value.some((item) => item.resourceId === id && item.resourceType === resourceType));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async function add(
|
|
379
|
+
resourceId: ProjectResourceId,
|
|
380
|
+
resourceType: string,
|
|
381
|
+
projectId = activeId.value,
|
|
382
|
+
/** `silent` : l'envoi groupe annonce son total, pas chaque fiche. */
|
|
383
|
+
options: { silent?: boolean } = {},
|
|
384
|
+
) {
|
|
385
|
+
if (!projectId) throw new Error(en.value ? "No active project" : "Aucun projet actif");
|
|
386
|
+
const ids = idList(resourceId);
|
|
387
|
+
if (!ids.length) throw new Error(en.value ? "Nothing to add" : "Rien à ajouter");
|
|
388
|
+
|
|
389
|
+
const user = (session.value as any)?.user;
|
|
390
|
+
// L'API identifie par `userId` : c'est le `sub` du jeton, pas l'email.
|
|
391
|
+
const addedUserId = user?.sub || user?.id;
|
|
392
|
+
if (!addedUserId) throw new Error(en.value ? "Sign in to add to a project" : "Connectez-vous pour ranger dans un projet");
|
|
393
|
+
|
|
394
|
+
let response: any;
|
|
395
|
+
try {
|
|
396
|
+
response = await api().projectResources.add(projectId, {
|
|
397
|
+
resources: ids.map((id) => ({ resourceId: id, resourceType })),
|
|
398
|
+
addedUserId,
|
|
399
|
+
});
|
|
400
|
+
} catch (error: any) {
|
|
401
|
+
if (!options.silent) notify(addFailure(error), false);
|
|
402
|
+
throw error;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const archived = archivedOf(response);
|
|
406
|
+
|
|
407
|
+
// Un ajout peut revenir « reussi » sans rien avoir ajoute (id absent de
|
|
408
|
+
// l'index) : sans ce garde-fou, la carte s'annoncerait rangee.
|
|
409
|
+
if (response && response.addedCount === 0 && !archived.length) {
|
|
410
|
+
const error = new Error(response?.notFound?.[0]?.esError || (en.value ? "The record was not added" : "La fiche n'a pas été ajoutée"));
|
|
411
|
+
if (!options.silent) notify(addFailure(error), false);
|
|
412
|
+
throw error;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (!options.silent && response?.addedCount) {
|
|
416
|
+
const where = nameOf(projectId);
|
|
417
|
+
notify(
|
|
418
|
+
en.value
|
|
419
|
+
? where ? `Added to ${where}` : "Added to the project"
|
|
420
|
+
: where ? `Ajouté à ${where}` : "Ajouté au projet",
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (projectId === activeId.value) {
|
|
425
|
+
const added = (response?.resources ?? []).map((r: any) => String(r.resourceId));
|
|
426
|
+
const kept = added.length ? added : ids.filter((id) => !archived.some((a) => a.resourceId === id));
|
|
427
|
+
items.value = [
|
|
428
|
+
...items.value,
|
|
429
|
+
...kept
|
|
430
|
+
.filter((id: string) => !has(id, resourceType))
|
|
431
|
+
.map((id: string) => ({ resourceId: id, resourceType, createdAt: new Date().toISOString() })),
|
|
432
|
+
];
|
|
433
|
+
itemsVersion++;
|
|
434
|
+
bumpCount(projectId, kept.length);
|
|
435
|
+
}
|
|
436
|
+
return response;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** Le compte de la liste suit les ajouts et retraits, sans relire la liste. */
|
|
440
|
+
function bumpCount(projectId: string, delta: number) {
|
|
441
|
+
projects.value = projects.value.map((project) =>
|
|
442
|
+
project.id === projectId && project.resourcesCount != null
|
|
443
|
+
? { ...project, resourcesCount: Math.max(0, project.resourcesCount + delta) }
|
|
444
|
+
: project,
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Ramene des fiches archivees, une famille a la fois. */
|
|
449
|
+
async function unarchive(
|
|
450
|
+
resourceId: ProjectResourceId,
|
|
451
|
+
resourceType: string,
|
|
452
|
+
projectId = activeId.value,
|
|
453
|
+
options: { silent?: boolean } = {},
|
|
454
|
+
) {
|
|
455
|
+
if (!projectId) throw new Error(en.value ? "No active project" : "Aucun projet actif");
|
|
456
|
+
const ids = idList(resourceId);
|
|
457
|
+
if (!ids.length) return null;
|
|
458
|
+
|
|
459
|
+
let response: any;
|
|
460
|
+
try {
|
|
461
|
+
response = await api().projectResources.unarchive(projectId, {
|
|
462
|
+
resourceIds: ids,
|
|
463
|
+
resourceType,
|
|
464
|
+
});
|
|
465
|
+
} catch (error) {
|
|
466
|
+
if (!options.silent) {
|
|
467
|
+
notify(en.value ? "The record could not be unarchived" : "La fiche n'a pas pu être désarchivée", false);
|
|
468
|
+
}
|
|
469
|
+
throw error;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (projectId === activeId.value) {
|
|
473
|
+
const back = ids.filter((id) => !has(id, resourceType));
|
|
474
|
+
items.value = [...items.value, ...back.map((id) => ({ resourceId: id, resourceType }))];
|
|
475
|
+
itemsVersion++;
|
|
476
|
+
bumpCount(projectId, back.length);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (!options.silent) {
|
|
480
|
+
const where = nameOf(projectId);
|
|
481
|
+
const count = Number(response?.unarchivedCount ?? ids.length);
|
|
482
|
+
notify(
|
|
483
|
+
en.value
|
|
484
|
+
? `${count} ${count > 1 ? "records" : "record"} brought back to ${where}`
|
|
485
|
+
: `${count} ${count > 1 ? "fiches remises" : "fiche remise"} dans ${where}`,
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
return response;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async function remove(
|
|
492
|
+
resourceId: ProjectResourceId,
|
|
493
|
+
resourceType: string,
|
|
494
|
+
projectId = activeId.value,
|
|
495
|
+
options: { silent?: boolean } = {},
|
|
496
|
+
) {
|
|
497
|
+
if (!projectId) throw new Error(en.value ? "No active project" : "Aucun projet actif");
|
|
498
|
+
const ids = idList(resourceId);
|
|
499
|
+
if (!ids.length) return null;
|
|
500
|
+
|
|
501
|
+
// Le type accompagne les ids : l'API ne retire que cette famille, sinon
|
|
502
|
+
// une fiche d'un autre index qui porte le meme id partirait avec.
|
|
503
|
+
let response: any;
|
|
504
|
+
try {
|
|
505
|
+
response = await api().projectResources.remove(projectId, {
|
|
506
|
+
resourceIds: ids,
|
|
507
|
+
resourceType,
|
|
508
|
+
});
|
|
509
|
+
} catch (error) {
|
|
510
|
+
if (!options.silent) notify(en.value ? "The record was not removed" : "La fiche n'a pas été retirée", false);
|
|
511
|
+
throw error;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (!options.silent) {
|
|
515
|
+
const where = nameOf(projectId);
|
|
516
|
+
notify(
|
|
517
|
+
en.value
|
|
518
|
+
? where ? `Removed from ${where}` : "Removed from the project"
|
|
519
|
+
: where ? `Retiré de ${where}` : "Retiré du projet",
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
if (projectId === activeId.value) {
|
|
523
|
+
items.value = items.value.filter(
|
|
524
|
+
(item) => !(ids.includes(item.resourceId) && item.resourceType === resourceType),
|
|
525
|
+
);
|
|
526
|
+
itemsVersion++;
|
|
527
|
+
}
|
|
528
|
+
bumpCount(projectId, -ids.length);
|
|
529
|
+
return response;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// ------------------------------------------------------- les fiches a vue
|
|
533
|
+
|
|
534
|
+
function registerAddable(key: string, entry: ProjectAddable) {
|
|
535
|
+
onPage.value = { ...onPage.value, [key]: entry };
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function unregisterAddable(key: string) {
|
|
539
|
+
if (!(key in onPage.value)) return;
|
|
540
|
+
const { [key]: _dropped, ...rest } = onPage.value;
|
|
541
|
+
onPage.value = rest;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** Les fiches de la page qui ne sont pas deja dans le projet actif. */
|
|
545
|
+
const pageAddable = computed(() =>
|
|
546
|
+
Object.values(onPage.value).filter((entry) => !has(entry.ids, entry.resourceType)),
|
|
547
|
+
);
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Range tout ce que la page affiche, une requete par famille. Rend les
|
|
551
|
+
* fiches refusees parce qu'archivees, nommees, pour que l'appelant propose
|
|
552
|
+
* de les remettre.
|
|
553
|
+
*/
|
|
554
|
+
async function addPage(projectId = activeId.value): Promise<RmProjectArchivedEntry[]> {
|
|
555
|
+
const entries = pageAddable.value;
|
|
556
|
+
if (!projectId || !entries.length) return [];
|
|
557
|
+
|
|
558
|
+
const byType = new Map<string, string[]>();
|
|
559
|
+
for (const entry of entries) {
|
|
560
|
+
byType.set(entry.resourceType, [...(byType.get(entry.resourceType) ?? []), ...entry.ids]);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// Le compte vient de l'API, pas du nombre de cartes : elle peut en refuser.
|
|
564
|
+
let added = 0;
|
|
565
|
+
let sent = 0;
|
|
566
|
+
let failedFamilies = 0;
|
|
567
|
+
const archived: RmProjectArchivedEntry[] = [];
|
|
568
|
+
|
|
569
|
+
for (const [resourceType, ids] of byType) {
|
|
570
|
+
sent += ids.length;
|
|
571
|
+
try {
|
|
572
|
+
const response: any = await add(ids, resourceType, projectId, { silent: true });
|
|
573
|
+
added += Number(response?.addedCount ?? 0);
|
|
574
|
+
for (const entry of archivedOf(response)) {
|
|
575
|
+
const card = entries.find(
|
|
576
|
+
(candidate) => candidate.resourceType === entry.resourceType && candidate.ids.includes(entry.resourceId),
|
|
577
|
+
);
|
|
578
|
+
archived.push({ key: `${entry.resourceType}:${entry.resourceId}`, label: card?.label || entry.resourceId });
|
|
579
|
+
}
|
|
580
|
+
} catch {
|
|
581
|
+
failedFamilies += 1;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const where = nameOf(projectId);
|
|
586
|
+
const refused = sent - added - archived.length;
|
|
587
|
+
if (added) {
|
|
588
|
+
const main = en.value
|
|
589
|
+
? `${added} ${added > 1 ? "records" : "record"} added to ${where}`
|
|
590
|
+
: `${added} ${added > 1 ? "fiches ajoutées" : "fiche ajoutée"} à ${where}`;
|
|
591
|
+
const rest = refused ? (en.value ? `, ${refused} refused` : `, ${refused} refusée${refused > 1 ? "s" : ""}`) : "";
|
|
592
|
+
notify(`${main}${rest}`, !refused);
|
|
593
|
+
} else if (!archived.length) {
|
|
594
|
+
notify(en.value ? "Nothing could be added" : "Aucune fiche n'a pu être ajoutée", failedFamilies === 0);
|
|
595
|
+
}
|
|
596
|
+
return archived;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** Remet des fiches archivees, une requete par famille. `key` vaut `type:id`. */
|
|
600
|
+
async function unarchiveEntries(entries: RmProjectArchivedEntry[], projectId = activeId.value) {
|
|
601
|
+
const byType = new Map<string, string[]>();
|
|
602
|
+
for (const entry of entries) {
|
|
603
|
+
const cut = entry.key.indexOf(":");
|
|
604
|
+
const type = entry.key.slice(0, cut);
|
|
605
|
+
byType.set(type, [...(byType.get(type) ?? []), entry.key.slice(cut + 1)]);
|
|
606
|
+
}
|
|
607
|
+
for (const [resourceType, ids] of byType) {
|
|
608
|
+
await unarchive(ids, resourceType, projectId, { silent: true });
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
return {
|
|
613
|
+
projects,
|
|
614
|
+
rmProjects,
|
|
615
|
+
toRm,
|
|
616
|
+
lang,
|
|
617
|
+
loaded,
|
|
618
|
+
pending,
|
|
619
|
+
failure,
|
|
620
|
+
active,
|
|
621
|
+
activeId,
|
|
622
|
+
pathOf,
|
|
623
|
+
isMine,
|
|
624
|
+
togglePin,
|
|
625
|
+
items,
|
|
626
|
+
itemsPending,
|
|
627
|
+
load,
|
|
628
|
+
create,
|
|
629
|
+
update,
|
|
630
|
+
destroy,
|
|
631
|
+
restoreActive,
|
|
632
|
+
setActive,
|
|
633
|
+
loadItems,
|
|
634
|
+
fetchItems,
|
|
635
|
+
has,
|
|
636
|
+
add,
|
|
637
|
+
unarchive,
|
|
638
|
+
unarchiveEntries,
|
|
639
|
+
archivedOf,
|
|
640
|
+
remove,
|
|
641
|
+
registerAddable,
|
|
642
|
+
unregisterAddable,
|
|
643
|
+
pageAddable,
|
|
644
|
+
addPage,
|
|
645
|
+
};
|
|
646
|
+
};
|