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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 7x 1x 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 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x | // src/services/db/storeLocation.db.ts
import type { Pool, PoolClient } from 'pg';
import { getPool } from './connection.db';
import type { Logger } from 'pino';
import { NotFoundError, handleDbError } from './errors.db';
import type { StoreLocation, Address, Store } from '../../types';
export interface StoreLocationWithAddress extends StoreLocation {
address: Address;
}
export interface StoreWithLocations extends Store {
locations: StoreLocationWithAddress[];
}
export class StoreLocationRepository {
private db: Pick<Pool | PoolClient, 'query'>;
constructor(db: Pick<Pool | PoolClient, 'query'> = getPool()) {
this.db = db;
}
/**
* Creates a link between a store and an address.
* @param storeId The store ID
* @param addressId The address ID
* @param logger Logger instance
* @returns The store_location_id of the created link
*/
async createStoreLocation(storeId: number, addressId: number, logger: Logger): Promise<number> {
try {
const query = `
INSERT INTO public.store_locations (store_id, address_id)
VALUES ($1, $2)
RETURNING store_location_id
`;
const result = await this.db.query<{ store_location_id: number }>(query, [
storeId,
addressId,
]);
return result.rows[0].store_location_id;
} catch (error) {
handleDbError(
error,
logger,
'Database error in createStoreLocation',
{ storeId, addressId },
{
uniqueMessage: 'This store is already linked to this address.',
defaultMessage: 'Failed to create store location link.',
},
);
}
}
/**
* Retrieves all locations (with address data) for a given store.
* @param storeId The store ID
* @param logger Logger instance
* @returns Array of StoreLocationWithAddress objects
*/
async getLocationsByStoreId(
storeId: number,
logger: Logger,
): Promise<StoreLocationWithAddress[]> {
try {
const query = `
SELECT
sl.*,
json_build_object(
'address_id', a.address_id,
'address_line_1', a.address_line_1,
'address_line_2', a.address_line_2,
'city', a.city,
'province_state', a.province_state,
'postal_code', a.postal_code,
'country', a.country,
'latitude', a.latitude,
'longitude', a.longitude,
'created_at', a.created_at,
'updated_at', a.updated_at
) as address
FROM public.store_locations sl
INNER JOIN public.addresses a ON sl.address_id = a.address_id
WHERE sl.store_id = $1
ORDER BY sl.created_at ASC
`;
const result = await this.db.query<StoreLocationWithAddress>(query, [storeId]);
return result.rows;
} catch (error) {
handleDbError(
error,
logger,
'Database error in getLocationsByStoreId',
{ storeId },
{
defaultMessage: 'Failed to retrieve store locations.',
},
);
}
}
/**
* Retrieves a store with all its locations (addresses included).
* @param storeId The store ID
* @param logger Logger instance
* @returns StoreWithLocations object
*/
async getStoreWithLocations(storeId: number, logger: Logger): Promise<StoreWithLocations> {
try {
const query = `
SELECT
s.*,
COALESCE(
json_agg(
json_build_object(
'store_location_id', sl.store_location_id,
'store_id', sl.store_id,
'address_id', sl.address_id,
'created_at', sl.created_at,
'updated_at', sl.updated_at,
'address', json_build_object(
'address_id', a.address_id,
'address_line_1', a.address_line_1,
'address_line_2', a.address_line_2,
'city', a.city,
'province_state', a.province_state,
'postal_code', a.postal_code,
'country', a.country,
'latitude', a.latitude,
'longitude', a.longitude,
'created_at', a.created_at,
'updated_at', a.updated_at
)
)
) FILTER (WHERE sl.store_location_id IS NOT NULL),
'[]'::json
) as locations
FROM public.stores s
LEFT JOIN public.store_locations sl ON s.store_id = sl.store_id
LEFT JOIN public.addresses a ON sl.address_id = a.address_id
WHERE s.store_id = $1
GROUP BY s.store_id
`;
const result = await this.db.query<StoreWithLocations>(query, [storeId]);
Iif (result.rowCount === 0) {
throw new NotFoundError(`Store with ID ${storeId} not found.`);
}
return result.rows[0];
} catch (error) {
handleDbError(
error,
logger,
'Database error in getStoreWithLocations',
{ storeId },
{
defaultMessage: 'Failed to retrieve store with locations.',
},
);
}
}
/**
* Retrieves all stores with their locations.
* @param logger Logger instance
* @returns Array of StoreWithLocations objects
*/
async getAllStoresWithLocations(logger: Logger): Promise<StoreWithLocations[]> {
try {
const query = `
SELECT
s.*,
COALESCE(
json_agg(
json_build_object(
'store_location_id', sl.store_location_id,
'store_id', sl.store_id,
'address_id', sl.address_id,
'created_at', sl.created_at,
'updated_at', sl.updated_at,
'address', json_build_object(
'address_id', a.address_id,
'address_line_1', a.address_line_1,
'address_line_2', a.address_line_2,
'city', a.city,
'province_state', a.province_state,
'postal_code', a.postal_code,
'country', a.country,
'latitude', a.latitude,
'longitude', a.longitude,
'created_at', a.created_at,
'updated_at', a.updated_at
)
)
) FILTER (WHERE sl.store_location_id IS NOT NULL),
'[]'::json
) as locations
FROM public.stores s
LEFT JOIN public.store_locations sl ON s.store_id = sl.store_id
LEFT JOIN public.addresses a ON sl.address_id = a.address_id
GROUP BY s.store_id
ORDER BY s.name ASC
`;
const result = await this.db.query<StoreWithLocations>(query);
return result.rows;
} catch (error) {
handleDbError(
error,
logger,
'Database error in getAllStoresWithLocations',
{},
{
defaultMessage: 'Failed to retrieve stores with locations.',
},
);
}
}
/**
* Deletes a store location link.
* @param storeLocationId The store_location_id to delete
* @param logger Logger instance
*/
async deleteStoreLocation(storeLocationId: number, logger: Logger): Promise<void> {
try {
const query = 'DELETE FROM public.store_locations WHERE store_location_id = $1';
const result = await this.db.query(query, [storeLocationId]);
if (result.rowCount === 0) {
throw new NotFoundError(`Store location with ID ${storeLocationId} not found.`);
}
} catch (error) {
handleDbError(
error,
logger,
'Database error in deleteStoreLocation',
{ storeLocationId },
{
defaultMessage: 'Failed to delete store location.',
},
);
}
}
/**
* Updates a store location to point to a different address.
* @param storeLocationId The store_location_id to update
* @param newAddressId The new address ID
* @param logger Logger instance
*/
async updateStoreLocation(
storeLocationId: number,
newAddressId: number,
logger: Logger,
): Promise<void> {
try {
const query = `
UPDATE public.store_locations
SET address_id = $1, updated_at = now()
WHERE store_location_id = $2
`;
const result = await this.db.query(query, [newAddressId, storeLocationId]);
Iif (result.rowCount === 0) {
throw new NotFoundError(`Store location with ID ${storeLocationId} not found.`);
}
} catch (error) {
handleDbError(
error,
logger,
'Database error in updateStoreLocation',
{ storeLocationId, newAddressId },
{
defaultMessage: 'Failed to update store location.',
},
);
}
}
}
|