-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add project and associations image management #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,18 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| import { asc, eq } from "drizzle-orm" | ||||||||||||||||||||||||||||||||||||||||||||
| import z from "zod" | ||||||||||||||||||||||||||||||||||||||||||||
| import { uploadBlob } from "@/azure/blob" | ||||||||||||||||||||||||||||||||||||||||||||
| import { DB, SCHEMA } from "@/db" | ||||||||||||||||||||||||||||||||||||||||||||
| import { createTRPCRouter, publicProcedure } from "@/trpc" | ||||||||||||||||||||||||||||||||||||||||||||
| import { getImageExtension } from "@/utils/web" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const ASSOCIATIONS = SCHEMA.WEB.associations | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const logoFileSchema = z | ||||||||||||||||||||||||||||||||||||||||||||
| .file() | ||||||||||||||||||||||||||||||||||||||||||||
| .mime(["image/png", "image/jpeg", "image/svg+xml"]) | ||||||||||||||||||||||||||||||||||||||||||||
| .min(1) | ||||||||||||||||||||||||||||||||||||||||||||
| .max(1024 * 1024) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const associationLinksSchema = z.object({ | ||||||||||||||||||||||||||||||||||||||||||||
| email: z.email().nullable(), | ||||||||||||||||||||||||||||||||||||||||||||
| website: z.url().nullable(), | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,6 +26,13 @@ const associationLinksSchema = z.object({ | |||||||||||||||||||||||||||||||||||||||||||
| spotify: z.url().nullable(), | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const associationFormSchema = z.object({ | ||||||||||||||||||||||||||||||||||||||||||||
| name: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionIt: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionEn: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| logo: logoFileSchema.optional(), | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const associationSchema = z.object({ | ||||||||||||||||||||||||||||||||||||||||||||
| id: z.number(), | ||||||||||||||||||||||||||||||||||||||||||||
| name: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -58,23 +73,28 @@ export default createTRPCRouter({ | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| addAssociation: publicProcedure | ||||||||||||||||||||||||||||||||||||||||||||
| .input( | ||||||||||||||||||||||||||||||||||||||||||||
| z.object({ | ||||||||||||||||||||||||||||||||||||||||||||
| name: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionIt: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionEn: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| logo: z.string().nullable(), | ||||||||||||||||||||||||||||||||||||||||||||
| createdBy: z.number(), | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| z | ||||||||||||||||||||||||||||||||||||||||||||
| .instanceof(FormData) | ||||||||||||||||||||||||||||||||||||||||||||
| .transform((fd): Record<string, string | File> => Object.fromEntries(fd.entries())) | ||||||||||||||||||||||||||||||||||||||||||||
| .pipe( | ||||||||||||||||||||||||||||||||||||||||||||
| associationFormSchema.extend({ | ||||||||||||||||||||||||||||||||||||||||||||
| createdBy: z.coerce.number<string>(), | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| .mutation(async ({ input }) => { | ||||||||||||||||||||||||||||||||||||||||||||
| const { name, descriptionIt, descriptionEn, logo, createdBy } = input | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const uploadedLogo = logo | ||||||||||||||||||||||||||||||||||||||||||||
| ? await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type) | ||||||||||||||||||||||||||||||||||||||||||||
|
BIA3IA marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||
| : null | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const [res] = await DB.insert(ASSOCIATIONS) | ||||||||||||||||||||||||||||||||||||||||||||
| .values({ | ||||||||||||||||||||||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionIt, | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionEn, | ||||||||||||||||||||||||||||||||||||||||||||
| logo, | ||||||||||||||||||||||||||||||||||||||||||||
| logo: uploadedLogo?.url ?? null, | ||||||||||||||||||||||||||||||||||||||||||||
| createdBy, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| .returning() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -84,24 +104,29 @@ export default createTRPCRouter({ | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| editAssociation: publicProcedure | ||||||||||||||||||||||||||||||||||||||||||||
| .input( | ||||||||||||||||||||||||||||||||||||||||||||
| z.object({ | ||||||||||||||||||||||||||||||||||||||||||||
| id: z.number(), | ||||||||||||||||||||||||||||||||||||||||||||
| name: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionIt: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionEn: z.string(), | ||||||||||||||||||||||||||||||||||||||||||||
| logo: z.string().nullable(), | ||||||||||||||||||||||||||||||||||||||||||||
| modifiedBy: z.number(), | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| z | ||||||||||||||||||||||||||||||||||||||||||||
| .instanceof(FormData) | ||||||||||||||||||||||||||||||||||||||||||||
| .transform((fd): Record<string, string | File> => Object.fromEntries(fd.entries())) | ||||||||||||||||||||||||||||||||||||||||||||
| .pipe( | ||||||||||||||||||||||||||||||||||||||||||||
| associationFormSchema.extend({ | ||||||||||||||||||||||||||||||||||||||||||||
| id: z.coerce.number<string>(), | ||||||||||||||||||||||||||||||||||||||||||||
| modifiedBy: z.coerce.number<string>(), | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| .mutation(async ({ input }) => { | ||||||||||||||||||||||||||||||||||||||||||||
| const { id, name, descriptionIt, descriptionEn, logo, modifiedBy } = input | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const uploadedLogo = logo | ||||||||||||||||||||||||||||||||||||||||||||
| ? await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type) | ||||||||||||||||||||||||||||||||||||||||||||
| : null | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| const [res] = await DB.update(ASSOCIATIONS) | ||||||||||||||||||||||||||||||||||||||||||||
| .set({ | ||||||||||||||||||||||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionIt, | ||||||||||||||||||||||||||||||||||||||||||||
| descriptionEn, | ||||||||||||||||||||||||||||||||||||||||||||
| logo, | ||||||||||||||||||||||||||||||||||||||||||||
| logo: uploadedLogo?.url ?? null, | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+120
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Preserve the existing logo on edit when no new file is uploaded.
Proposed fix- const uploadedLogo = logo
- ? await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type)
- : null
+ const uploadedLogoUrl = logo
+ ? (await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type)).url
+ : undefined
const [res] = await DB.update(ASSOCIATIONS)
.set({
name,
descriptionIt,
descriptionEn,
- logo: uploadedLogo?.url ?? null,
+ ...(uploadedLogoUrl !== undefined ? { logo: uploadedLogoUrl } : {}),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| modifiedBy, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| .where(eq(ASSOCIATIONS.id, id)) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.