some more re-org + fixes
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 2m13s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 2m13s
This commit is contained in:
@@ -126,32 +126,30 @@ export async function findFlyerByChecksum(checksum: string): Promise<Flyer | und
|
||||
*/
|
||||
// prettier-ignore
|
||||
export async function createFlyerAndItems(
|
||||
flyerData: Omit<Flyer, 'flyer_id' | 'created_at' | 'store'> & { store_name: string },
|
||||
flyerData: Omit<Flyer, 'flyer_id' | 'created_at' | 'store'>,
|
||||
items: Omit<FlyerItem, 'flyer_item_id' | 'flyer_id' | 'created_at'>[]
|
||||
): Promise<Flyer> {
|
||||
const client = await getPool().connect();
|
||||
logger.debug('[DB createFlyerAndItems] Starting transaction to create flyer.', { flyerData: { name: flyerData.file_name, store: flyerData.store_name }, itemCount: items.length });
|
||||
logger.debug('[DB createFlyerAndItems] Starting transaction to create flyer.', { flyerData: { name: flyerData.file_name, store_id: flyerData.store_id }, itemCount: items.length });
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
logger.debug('[DB createFlyerAndItems] BEGIN transaction successful.');
|
||||
|
||||
// Find or create the store
|
||||
let storeId: number;
|
||||
const storeRes = await client.query<{ store_id: number }>('SELECT store_id FROM public.stores WHERE name = $1', [flyerData.store_name]);
|
||||
if (storeRes.rows.length > 0) {
|
||||
storeId = storeRes.rows[0].store_id;
|
||||
} else {
|
||||
const newStoreRes = await client.query<{ store_id: number }>('INSERT INTO public.stores (name) VALUES ($1) RETURNING store_id', [flyerData.store_name]);
|
||||
storeId = newStoreRes.rows[0].store_id;
|
||||
// The store_id is now expected to be passed in the flyerData object,
|
||||
// as it's resolved in the route handler.
|
||||
const storeId = flyerData.store_id;
|
||||
if (!storeId) {
|
||||
throw new Error("store_id is required to create a flyer.");
|
||||
}
|
||||
|
||||
// Create the flyer record
|
||||
const flyerQuery = `
|
||||
INSERT INTO public.flyers (file_name, image_url, checksum, store_id, valid_from, valid_to, store_address)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
INSERT INTO public.flyers (file_name, image_url, checksum, store_id, valid_from, valid_to, store_address, uploaded_by)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING *;
|
||||
`;
|
||||
const flyerValues = [flyerData.file_name, flyerData.image_url, flyerData.checksum, storeId, flyerData.valid_from, flyerData.valid_to, flyerData.store_address];
|
||||
const flyerValues = [flyerData.file_name, flyerData.image_url, flyerData.checksum, storeId, flyerData.valid_from, flyerData.valid_to, flyerData.store_address, flyerData.uploaded_by];
|
||||
const newFlyerRes = await client.query<Flyer>(flyerQuery, flyerValues);
|
||||
const newFlyer = newFlyerRes.rows[0];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user