|
| 1 | +import { Device, User } from '~/schema' |
| 2 | +import { |
| 3 | + deleteDevice as deleteDeviceById, |
| 4 | +} from '~/models/device.server' |
| 5 | +import { verifyLogin } from '~/models/user.server' |
| 6 | +import { z } from 'zod' |
| 7 | + |
| 8 | +export const BoxesQuerySchema = z.object({ |
| 9 | + format: z.enum(["json", "geojson"] ,{ |
| 10 | + errorMap: () => ({ message: "Format must be either 'json' or 'geojson'" }), |
| 11 | + }).default("json"), |
| 12 | + minimal: z.enum(["true", "false"]).default("false") |
| 13 | + .transform((v) => v === "true"), |
| 14 | + full: z.enum(["true", "false"]).default("false") |
| 15 | + .transform((v) => v === "true"), |
| 16 | + limit: z |
| 17 | + .string() |
| 18 | + .default("5") |
| 19 | + .transform((val) => parseInt(val, 10)) |
| 20 | + .refine((val) => !isNaN(val), { message: "Limit must be a number" }) |
| 21 | + .refine((val) => val >= 1, { message: "Limit must be at least 1" }) |
| 22 | + .refine((val) => val <= 20, { message: "Limit must not exceed 20" }), |
| 23 | + |
| 24 | + name: z.string().optional(), |
| 25 | + date: z.preprocess( |
| 26 | + (val) => { |
| 27 | + if (typeof val === "string") return [val]; |
| 28 | + if (Array.isArray(val)) return val; |
| 29 | + return val; |
| 30 | + }, |
| 31 | + z.array(z.string()) |
| 32 | + .min(1, "At least one date required") |
| 33 | + .max(2, "At most two dates allowed") |
| 34 | + .transform((arr) => { |
| 35 | + const [fromDateStr, toDateStr] = arr; |
| 36 | + const fromDate = new Date(fromDateStr); |
| 37 | + if (isNaN(fromDate.getTime())) throw new Error(`Invalid date: ${fromDateStr}`); |
| 38 | + |
| 39 | + if (!toDateStr) { |
| 40 | + return { |
| 41 | + fromDate: new Date(fromDate.getTime() - 4 * 60 * 60 * 1000), |
| 42 | + toDate: new Date(fromDate.getTime() + 4 * 60 * 60 * 1000), |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + const toDate = new Date(toDateStr); |
| 47 | + if (isNaN(toDate.getTime())) throw new Error(`Invalid date: ${toDateStr}`); |
| 48 | + return { fromDate, toDate }; |
| 49 | + }) |
| 50 | + ).optional(), |
| 51 | + phenomenon: z.string().optional(), |
| 52 | + grouptag: z.string().transform((v) => [v]).optional(), |
| 53 | + model: z.string().transform((v) => [v]).optional(), |
| 54 | + exposure: z.string().transform((v) => [v]).optional(), |
| 55 | + |
| 56 | + near: z |
| 57 | + .string() |
| 58 | + .regex(/^[-+]?\d+(\.\d+)?,[-+]?\d+(\.\d+)?$/, { |
| 59 | + message: "Invalid 'near' parameter format. Expected: 'lat,lng'", |
| 60 | + }) |
| 61 | + .transform((val) => val.split(",").map(Number) as [number, number]) |
| 62 | + .optional(), |
| 63 | + |
| 64 | + maxDistance: z.string().transform((v) => Number(v)).optional(), |
| 65 | + |
| 66 | + bbox: z |
| 67 | + .string() |
| 68 | + .transform((val) => { |
| 69 | + const coords = val.split(",").map(Number); |
| 70 | + if (coords.length !== 4 || coords.some((n) => isNaN(n))) { |
| 71 | + throw new Error("Invalid bbox parameter"); |
| 72 | + } |
| 73 | + const [swLng, swLat, neLng, neLat] = coords; |
| 74 | + return { |
| 75 | + coordinates: [ |
| 76 | + [ |
| 77 | + [swLat, swLng], |
| 78 | + [neLat, swLng], |
| 79 | + [neLat, neLng], |
| 80 | + [swLat, neLng], |
| 81 | + [swLat, swLng], |
| 82 | + ], |
| 83 | + ], |
| 84 | + }; |
| 85 | + }) |
| 86 | + .optional(), |
| 87 | + |
| 88 | + fromDate: z.string().datetime().transform((v) => new Date(v)).optional(), |
| 89 | + toDate: z.string().datetime().transform((v) => new Date(v)).optional(), |
| 90 | + }) |
| 91 | +// .refine( |
| 92 | +// (data) => |
| 93 | +// !(data.date && !data.phenomenon) && !(data.phenomenon && !data.date), |
| 94 | +// { |
| 95 | +// message: "Date and phenomenon must be used together", |
| 96 | +// path: ["date"], |
| 97 | +// } |
| 98 | +// ); |
| 99 | + |
| 100 | + |
| 101 | + export type BoxesQueryParams = z.infer<typeof BoxesQuerySchema>; |
| 102 | + |
| 103 | +/** |
| 104 | + * Deletes a device after verifiying that the user is entitled by checking |
| 105 | + * the password. |
| 106 | + * @param user The user deleting the device |
| 107 | + * @param password The users password to verify |
| 108 | + * @returns True if the device was deleted, otherwise false or "unauthorized" |
| 109 | + * if the user is not entitled to delete the device with the given parameters |
| 110 | + */ |
| 111 | +export const deleteDevice = async ( |
| 112 | + user: User, |
| 113 | + device: Device, |
| 114 | + password: string, |
| 115 | +): Promise<boolean | 'unauthorized'> => { |
| 116 | + const verifiedUser = await verifyLogin(user.email, password) |
| 117 | + if (verifiedUser === null) return 'unauthorized' |
| 118 | + return (await deleteDeviceById({ id: device.id })).count > 0 |
| 119 | +} |
0 commit comments