@@ -0,0 +1,20 @@
|
||||
import { asc, eq } from 'drizzle-orm';
|
||||
|
||||
import { categories, conditionProfiles, conditions } from '../../../infrastructure/database/schema.js';
|
||||
|
||||
export function createCatalogLookupRepository(db) {
|
||||
return {
|
||||
async listCategories() {
|
||||
return db.select({ id: categories.id, name: categories.name }).from(categories).where(eq(categories.active, true)).orderBy(asc(categories.sort), asc(categories.name));
|
||||
},
|
||||
|
||||
async listConditions() {
|
||||
return db
|
||||
.select({ id: conditions.id, name: conditions.name, code: conditions.code, categoryId: conditionProfiles.categoryId })
|
||||
.from(conditions)
|
||||
.innerJoin(conditionProfiles, eq(conditionProfiles.id, conditions.conditionProfileId))
|
||||
.where(eq(conditions.active, true))
|
||||
.orderBy(asc(conditions.qualityRank), asc(conditions.name));
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
export async function registerAdminLookupRoutes(app, options) {
|
||||
const { authenticateAdmin, catalogLookupRepository } = options;
|
||||
|
||||
app.addHook('preHandler', async (request) => {
|
||||
const admin = await authenticateAdmin(request);
|
||||
if (!admin?.sellerId) {
|
||||
const error = new Error('Authentication is required.');
|
||||
error.code = 'UNAUTHORIZED';
|
||||
error.statusCode = 401;
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/lookups/categories', async () => ({ data: catalogLookupRepository.listCategories() }));
|
||||
app.get('/lookups/conditions', async () => ({ data: catalogLookupRepository.listConditions() }));
|
||||
}
|
||||
Reference in New Issue
Block a user