|
| 1 | +import type { ManufacturerRequestType } from '@stacksjs/orm' |
| 2 | +import type { ManufacturerJsonResponse } from '../../../../orm/src/models/Manufacturer' |
| 3 | +import { db } from '@stacksjs/database' |
| 4 | +import { fetchById } from './fetch' |
| 5 | + |
| 6 | +/** |
| 7 | + * Update a manufacturer by ID |
| 8 | + * |
| 9 | + * @param id The ID of the manufacturer to update |
| 10 | + * @param request The updated manufacturer data |
| 11 | + * @returns The updated manufacturer record |
| 12 | + */ |
| 13 | +export async function update(id: number, request: ManufacturerRequestType): Promise<ManufacturerJsonResponse | undefined> { |
| 14 | + // Validate the request data |
| 15 | + await request.validate() |
| 16 | + |
| 17 | + // Check if manufacturer exists |
| 18 | + const existingManufacturer = await fetchById(id) |
| 19 | + if (!existingManufacturer) { |
| 20 | + throw new Error(`Manufacturer with ID ${id} not found`) |
| 21 | + } |
| 22 | + |
| 23 | + // Create update data object using request fields |
| 24 | + const updateData: Record<string, any> = { |
| 25 | + manufacturer: request.get<string>('manufacturer'), |
| 26 | + description: request.get<string>('description'), |
| 27 | + country: request.get<string>('country'), |
| 28 | + featured: request.get<boolean>('featured'), |
| 29 | + updated_at: new Date(), |
| 30 | + } |
| 31 | + |
| 32 | + // Remove undefined fields to avoid overwriting with null values |
| 33 | + Object.keys(updateData).forEach((key) => { |
| 34 | + if (updateData[key] === undefined) { |
| 35 | + delete updateData[key] |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + // If no fields to update, just return the existing manufacturer |
| 40 | + if (Object.keys(updateData).length === 1) { // Only updated_at was set |
| 41 | + return existingManufacturer |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + // Update the manufacturer |
| 46 | + await db |
| 47 | + .updateTable('manufacturers') |
| 48 | + .set(updateData) |
| 49 | + .where('id', '=', id) |
| 50 | + .execute() |
| 51 | + |
| 52 | + // Fetch and return the updated manufacturer |
| 53 | + return await fetchById(id) |
| 54 | + } |
| 55 | + catch (error) { |
| 56 | + if (error instanceof Error) { |
| 57 | + // Handle duplicate name error if you have unique constraints |
| 58 | + if (error.message.includes('Duplicate entry') && error.message.includes('manufacturer')) { |
| 59 | + throw new Error('A manufacturer with this name already exists') |
| 60 | + } |
| 61 | + |
| 62 | + throw new Error(`Failed to update manufacturer: ${error.message}`) |
| 63 | + } |
| 64 | + |
| 65 | + throw error |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Toggle the featured status of a manufacturer |
| 71 | + * |
| 72 | + * @param id The ID of the manufacturer |
| 73 | + * @param featured The new featured status (or toggle if not provided) |
| 74 | + * @returns The updated manufacturer with the new featured status |
| 75 | + */ |
| 76 | +export async function updateFeaturedStatus( |
| 77 | + id: number, |
| 78 | + featured?: boolean, |
| 79 | +): Promise<ManufacturerJsonResponse | undefined> { |
| 80 | + // Check if manufacturer exists |
| 81 | + const manufacturer = await fetchById(id) |
| 82 | + |
| 83 | + if (!manufacturer) { |
| 84 | + throw new Error(`Manufacturer with ID ${id} not found`) |
| 85 | + } |
| 86 | + |
| 87 | + // If featured is not provided, toggle the current value |
| 88 | + const newFeaturedStatus = featured !== undefined ? featured : !manufacturer.featured |
| 89 | + |
| 90 | + try { |
| 91 | + // Update the manufacturer featured status |
| 92 | + await db |
| 93 | + .updateTable('manufacturers') |
| 94 | + .set({ |
| 95 | + featured: newFeaturedStatus, |
| 96 | + updated_at: new Date(), |
| 97 | + }) |
| 98 | + .where('id', '=', id) |
| 99 | + .execute() |
| 100 | + |
| 101 | + // Fetch the updated manufacturer |
| 102 | + return await fetchById(id) |
| 103 | + } |
| 104 | + catch (error) { |
| 105 | + if (error instanceof Error) { |
| 106 | + throw new TypeError(`Failed to update manufacturer featured status: ${error.message}`) |
| 107 | + } |
| 108 | + |
| 109 | + throw error |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Update a manufacturer by UUID |
| 115 | + * |
| 116 | + * @param uuid The UUID of the manufacturer to update |
| 117 | + * @param request The updated manufacturer data |
| 118 | + * @returns The updated manufacturer record |
| 119 | + */ |
| 120 | +export async function updateByUuid(uuid: string, request: ManufacturerRequestType): Promise<ManufacturerJsonResponse | undefined> { |
| 121 | + try { |
| 122 | + // Find the manufacturer by UUID |
| 123 | + const manufacturer = await db |
| 124 | + .selectFrom('manufacturers') |
| 125 | + .where('uuid', '=', uuid) |
| 126 | + .selectAll() |
| 127 | + .executeTakeFirst() |
| 128 | + |
| 129 | + if (!manufacturer) { |
| 130 | + throw new Error(`Manufacturer with UUID ${uuid} not found`) |
| 131 | + } |
| 132 | + |
| 133 | + // Use the regular update function with the ID |
| 134 | + return await update(manufacturer.id, request) |
| 135 | + } |
| 136 | + catch (error) { |
| 137 | + if (error instanceof Error) { |
| 138 | + throw new TypeError(`Failed to update manufacturer: ${error.message}`) |
| 139 | + } |
| 140 | + |
| 141 | + throw error |
| 142 | + } |
| 143 | +} |
0 commit comments