working ! testing !
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 2m31s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 2m31s
This commit is contained in:
@@ -126,7 +126,7 @@ export async function findFlyerByChecksum(checksum: string): Promise<Flyer | und
|
||||
*/
|
||||
// prettier-ignore
|
||||
export async function createFlyerAndItems(
|
||||
flyerData: Omit<Flyer, 'flyer_id' | 'created_at' | 'store'>,
|
||||
flyerData: Omit<Flyer, 'flyer_id' | 'created_at' | 'store' | 'store_id'> & { store_name: string },
|
||||
items: Omit<FlyerItem, 'flyer_item_id' | 'flyer_id' | 'created_at'>[]
|
||||
): Promise<Flyer> {
|
||||
const client = await getPool().connect();
|
||||
@@ -135,12 +135,14 @@ export async function createFlyerAndItems(
|
||||
await client.query('BEGIN');
|
||||
logger.debug('[DB createFlyerAndItems] BEGIN transaction successful.');
|
||||
|
||||
// Find or create the store
|
||||
// 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.");
|
||||
// Find or create the store to get its ID. This logic is now self-contained.
|
||||
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;
|
||||
}
|
||||
|
||||
// Create the flyer record
|
||||
|
||||
Reference in New Issue
Block a user