|
| 1 | +import { ActionError, defineAction } from "astro:actions"; |
| 2 | +import * as z from "astro/zod"; |
| 3 | +import { DataProvenSchema } from "#schemas"; |
| 4 | +import { saveToCloud } from "#utils/gitlab"; |
| 5 | +import { getDisprovenToSave, getProvenToSave } from "#utils/submit-elements"; |
| 6 | + |
| 7 | +const submitDisprovenElements = defineAction({ |
| 8 | + input: z.object({ elements: z.array(z.object({ name: z.string() })) }), |
| 9 | + async handler(input, context) { |
| 10 | + const role = null; |
| 11 | + |
| 12 | + if (!role) { |
| 13 | + throw new ActionError({ |
| 14 | + code: "UNAUTHORIZED", |
| 15 | + message: "Please login to use this feature.", |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + const canSubmitElements = false; |
| 20 | + |
| 21 | + if (!canSubmitElements) { |
| 22 | + throw new ActionError({ |
| 23 | + code: "FORBIDDEN", |
| 24 | + message: "You don't have permissions to use this feature.", |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + const elements = input.elements.map((element) => element.name); |
| 29 | + |
| 30 | + const data = await getDisprovenToSave(elements); |
| 31 | + await saveToCloud(data); |
| 32 | + |
| 33 | + return { success: true } as const; |
| 34 | + }, |
| 35 | +}); |
| 36 | + |
| 37 | +const submitProvenElements = defineAction({ |
| 38 | + input: z.object({ elements: DataProvenSchema }), |
| 39 | + handler: async (input, context) => { |
| 40 | + const role = null; |
| 41 | + |
| 42 | + if (!role) { |
| 43 | + throw new ActionError({ |
| 44 | + code: "UNAUTHORIZED", |
| 45 | + message: "Please login to use this feature.", |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + const canSubmitElements = false; |
| 50 | + |
| 51 | + if (!canSubmitElements) { |
| 52 | + throw new ActionError({ |
| 53 | + code: "FORBIDDEN", |
| 54 | + message: "You don't have permissions to use this feature.", |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + const data = await getProvenToSave(input.elements); |
| 59 | + await saveToCloud(data); |
| 60 | + |
| 61 | + return { success: true } as const; |
| 62 | + }, |
| 63 | +}); |
| 64 | + |
| 65 | +export { submitDisprovenElements, submitProvenElements }; |
0 commit comments