@@ -0,0 +1,24 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
import { sellerSettings } from '../../../infrastructure/database/schema.js';
|
||||
|
||||
export function createSellerSettingsRepository(db) {
|
||||
return {
|
||||
async findBySellerId(sellerId) {
|
||||
const [record] = await db.select().from(sellerSettings).where(eq(sellerSettings.sellerId, sellerId));
|
||||
return record ?? null;
|
||||
},
|
||||
|
||||
async upsert(sellerId, settings) {
|
||||
const [record] = await db
|
||||
.insert(sellerSettings)
|
||||
.values({ sellerId, settings })
|
||||
.onConflictDoUpdate({
|
||||
target: sellerSettings.sellerId,
|
||||
set: { settings, updatedAt: new Date() },
|
||||
})
|
||||
.returning();
|
||||
return record;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { sellerSettingsSchema } from '@pokemon-inventory/validation/settings';
|
||||
|
||||
import { createApplicationError } from '../../../shared/errors/application-error.js';
|
||||
|
||||
export async function registerAdminSettingsRoutes(app, options) {
|
||||
const {
|
||||
authenticateAdmin = async () => {
|
||||
throw createApplicationError('UNAUTHORIZED', 'Authentication is required.', 401);
|
||||
},
|
||||
authorizeUnsafeRequest,
|
||||
sessionCookieName = 'pokemon_inventory_session',
|
||||
sellerSettingsService,
|
||||
} = options;
|
||||
|
||||
app.addHook('preHandler', async (request) => {
|
||||
const admin = await authenticateAdmin(request);
|
||||
if (!admin?.sellerId) {
|
||||
throw createApplicationError('UNAUTHORIZED', 'Authentication is required.', 401);
|
||||
}
|
||||
request.admin = admin;
|
||||
});
|
||||
|
||||
app.get('/settings', async (request) => ({
|
||||
data: await sellerSettingsService.get({ sellerId: request.admin.sellerId }),
|
||||
}));
|
||||
|
||||
app.patch('/settings', async (request, reply) => {
|
||||
await authorizeUnsafeRequest?.(
|
||||
request.cookies[sessionCookieName],
|
||||
request.headers['x-csrf-token'],
|
||||
);
|
||||
const result = sellerSettingsSchema.safeParse(request.body);
|
||||
if (!result.success) {
|
||||
return reply.code(400).send({
|
||||
error: {
|
||||
code: 'VALIDATION_ERROR',
|
||||
message: 'Request validation failed.',
|
||||
details: result.error.issues.map(({ message, path }) => ({ message, path })),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return reply.send({
|
||||
data: await sellerSettingsService.update({
|
||||
sellerId: request.admin.sellerId,
|
||||
settings: result.data,
|
||||
}),
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { sellerSettingsSchema } from '@pokemon-inventory/validation/settings';
|
||||
|
||||
export function createSellerSettingsService({ sellerSettingsRepository }) {
|
||||
return {
|
||||
async get({ sellerId }) {
|
||||
const record = await sellerSettingsRepository.findBySellerId(sellerId);
|
||||
return record?.settings ?? {};
|
||||
},
|
||||
|
||||
async update({ sellerId, settings }) {
|
||||
const validated = sellerSettingsSchema.parse(settings);
|
||||
const current = await this.get({ sellerId });
|
||||
const merged = mergeSettings(current, validated);
|
||||
const record = await sellerSettingsRepository.upsert(sellerId, merged);
|
||||
return record.settings;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function mergeSettings(current, update) {
|
||||
return Object.fromEntries(
|
||||
Object.entries({ ...current, ...update }).map(([category, values]) => [
|
||||
category,
|
||||
{ ...(current[category] ?? {}), ...values },
|
||||
]),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user