Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | // src/services/db/flyerLocation.db.ts
/**
* Repository for managing flyer_locations (many-to-many relationship between flyers and store_locations).
*/
import type { Logger } from 'pino';
import type { PoolClient, Pool } from 'pg';
import { handleDbError } from './errors.db';
import type { FlyerLocation } from '../../types';
import { getPool } from './connection.db';
export class FlyerLocationRepository {
private db: Pool | PoolClient;
constructor(dbClient?: PoolClient) {
this.db = dbClient || getPool();
}
/**
* Links a flyer to one or more store locations.
* @param flyerId The ID of the flyer
* @param storeLocationIds Array of store_location_ids to associate with this flyer
* @param logger Logger instance
* @returns Promise that resolves when all links are created
*/
async linkFlyerToLocations(
flyerId: number,
storeLocationIds: number[],
logger: Logger,
): Promise<void> {
try {
if (storeLocationIds.length === 0) {
logger.warn({ flyerId }, 'No store locations provided for flyer linkage');
return;
}
// Use VALUES with multiple rows for efficient bulk insert
const values = storeLocationIds.map((_, index) => `($1, $${index + 2})`).join(', ');
const query = `
INSERT INTO public.flyer_locations (flyer_id, store_location_id)
VALUES ${values}
ON CONFLICT (flyer_id, store_location_id) DO NOTHING
`;
await this.db.query(query, [flyerId, ...storeLocationIds]);
logger.info(
{ flyerId, locationCount: storeLocationIds.length },
'Linked flyer to store locations',
);
} catch (error) {
handleDbError(
error,
logger,
'Database error in linkFlyerToLocations',
{ flyerId, storeLocationIds },
{
defaultMessage: 'Failed to link flyer to store locations.',
},
);
}
}
/**
* Links a flyer to all locations of a given store.
* This is a convenience method for the common case where a flyer is valid at all store locations.
* @param flyerId The ID of the flyer
* @param storeId The ID of the store
* @param logger Logger instance
* @returns Promise that resolves to the number of locations linked
*/
async linkFlyerToAllStoreLocations(
flyerId: number,
storeId: number,
logger: Logger,
): Promise<number> {
try {
const query = `
INSERT INTO public.flyer_locations (flyer_id, store_location_id)
SELECT $1, store_location_id
FROM public.store_locations
WHERE store_id = $2
ON CONFLICT (flyer_id, store_location_id) DO NOTHING
RETURNING store_location_id
`;
const res = await this.db.query(query, [flyerId, storeId]);
const linkedCount = res.rowCount || 0;
logger.info({ flyerId, storeId, linkedCount }, 'Linked flyer to all store locations');
return linkedCount;
} catch (error) {
handleDbError(
error,
logger,
'Database error in linkFlyerToAllStoreLocations',
{ flyerId, storeId },
{
defaultMessage: 'Failed to link flyer to all store locations.',
},
);
}
}
/**
* Removes all location links for a flyer.
* @param flyerId The ID of the flyer
* @param logger Logger instance
*/
async unlinkAllLocations(flyerId: number, logger: Logger): Promise<void> {
try {
await this.db.query('DELETE FROM public.flyer_locations WHERE flyer_id = $1', [flyerId]);
logger.info({ flyerId }, 'Unlinked all locations from flyer');
} catch (error) {
handleDbError(
error,
logger,
'Database error in unlinkAllLocations',
{ flyerId },
{
defaultMessage: 'Failed to unlink locations from flyer.',
},
);
}
}
/**
* Removes a specific location link from a flyer.
* @param flyerId The ID of the flyer
* @param storeLocationId The ID of the store location to unlink
* @param logger Logger instance
*/
async unlinkLocation(flyerId: number, storeLocationId: number, logger: Logger): Promise<void> {
try {
await this.db.query(
'DELETE FROM public.flyer_locations WHERE flyer_id = $1 AND store_location_id = $2',
[flyerId, storeLocationId],
);
logger.info({ flyerId, storeLocationId }, 'Unlinked location from flyer');
} catch (error) {
handleDbError(
error,
logger,
'Database error in unlinkLocation',
{ flyerId, storeLocationId },
{
defaultMessage: 'Failed to unlink location from flyer.',
},
);
}
}
/**
* Gets all location IDs associated with a flyer.
* @param flyerId The ID of the flyer
* @param logger Logger instance
* @returns Promise that resolves to an array of store_location_ids
*/
async getLocationIdsByFlyerId(flyerId: number, logger: Logger): Promise<number[]> {
try {
const res = await this.db.query<{ store_location_id: number }>(
'SELECT store_location_id FROM public.flyer_locations WHERE flyer_id = $1',
[flyerId],
);
return res.rows.map((row) => row.store_location_id);
} catch (error) {
handleDbError(
error,
logger,
'Database error in getLocationIdsByFlyerId',
{ flyerId },
{
defaultMessage: 'Failed to get location IDs for flyer.',
},
);
}
}
/**
* Gets all flyer_location records for a flyer.
* @param flyerId The ID of the flyer
* @param logger Logger instance
* @returns Promise that resolves to an array of FlyerLocation objects
*/
async getFlyerLocationsByFlyerId(flyerId: number, logger: Logger): Promise<FlyerLocation[]> {
try {
const res = await this.db.query<FlyerLocation>(
'SELECT * FROM public.flyer_locations WHERE flyer_id = $1',
[flyerId],
);
return res.rows;
} catch (error) {
handleDbError(
error,
logger,
'Database error in getFlyerLocationsByFlyerId',
{ flyerId },
{
defaultMessage: 'Failed to get flyer locations.',
},
);
}
}
}
|