feat(F-048): completed feature
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { FastifySchema } from 'fastify';
|
||||
import type pg from 'pg';
|
||||
import { z } from 'zod';
|
||||
import type { Authenticate } from '../../../shared/auth.js';
|
||||
import { requireRole } from '../../../shared/auth.js';
|
||||
import { parseJson } from '../../../shared/http-input.js';
|
||||
import { AppError } from '../../../shared/errors.js';
|
||||
import { errorSchema } from '../../../shared/swagger.js';
|
||||
|
||||
export interface StoreSettingsRoutesDeps {
|
||||
pool: pg.Pool;
|
||||
@@ -38,7 +39,12 @@ export async function registerStoreSettingsRoutes(
|
||||
deps: StoreSettingsRoutesDeps,
|
||||
): Promise<void> {
|
||||
// GET /admin/settings — fetch all settings
|
||||
app.get('/admin/settings', async (request, reply) => {
|
||||
const getSettingsSchema: FastifySchema = {
|
||||
tags: ['Admin'],
|
||||
summary: 'Get store settings (admin)',
|
||||
response: { 401: errorSchema, 403: errorSchema },
|
||||
};
|
||||
app.get('/admin/settings', { schema: getSettingsSchema }, async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const result = await deps.pool.query<{ key: string; value: string }>(
|
||||
@@ -61,7 +67,13 @@ export async function registerStoreSettingsRoutes(
|
||||
});
|
||||
|
||||
// PATCH /admin/settings — update one or more settings
|
||||
app.patch('/admin/settings', async (request, reply) => {
|
||||
const patchSettingsSchema: FastifySchema = {
|
||||
tags: ['Admin'],
|
||||
summary: 'Update store settings (admin)',
|
||||
body: { type: 'object' },
|
||||
response: { 401: errorSchema, 403: errorSchema },
|
||||
};
|
||||
app.patch('/admin/settings', { schema: patchSettingsSchema }, async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const input = parseJson(updateSettingsSchema, request.body);
|
||||
@@ -94,7 +106,9 @@ export async function registerStoreSettingsRoutes(
|
||||
`SELECT key, value FROM store_settings`,
|
||||
);
|
||||
const map: Record<string, string> = {};
|
||||
for (const row of result.rows) { map[row.key] = row.value; }
|
||||
for (const row of result.rows) {
|
||||
map[row.key] = row.value;
|
||||
}
|
||||
return reply.send({
|
||||
storeName: map['store_name'] ?? '',
|
||||
storeTagline: map['store_tagline'] ?? '',
|
||||
|
||||
Reference in New Issue
Block a user