51 lines
2.6 KiB
JavaScript
51 lines
2.6 KiB
JavaScript
import { service as defaultService } from "./service.js";
|
|
import { parseWindow, parseTransactionsQuery, parseVerifyTargets, isUuid } from "./validate.js";
|
|
|
|
// `input` is the request body on the API-key path and the query string on the
|
|
// browser path; both are plain string maps, so one parser serves both.
|
|
// `status` is for the browser routes — the API-key router ignores it.
|
|
export function makeDashboardApi(service = defaultService) {
|
|
return {
|
|
async summary(input, userId) {
|
|
return { message: "dashboard summary", status: 200, data: await service.summary(userId, parseWindow(input)) };
|
|
},
|
|
|
|
async transactions(input, userId) {
|
|
const parsed = parseTransactionsQuery(input);
|
|
if (!parsed.ok) return { message: "invalid request", status: 400, data: { error: parsed.error } };
|
|
return { message: "dashboard transactions", status: 200, data: await service.transactions(userId, parsed.value) };
|
|
},
|
|
|
|
async series(input, userId) {
|
|
return { message: "dashboard series", status: 200, data: await service.series(userId, parseWindow(input)) };
|
|
},
|
|
|
|
async verify(input, userId) {
|
|
return { message: "dashboard verify", status: 200, data: await service.verify(userId, parseVerifyTargets(input)) };
|
|
},
|
|
|
|
async event(input, userId) {
|
|
if (!isUuid(input?.eventUuid)) return { message: "invalid request", status: 400, data: { error: "invalid event id" } };
|
|
const record = await service.event(userId, input.eventUuid);
|
|
if (!record) return { message: "dashboard event", status: 404, data: { error: "event not found" } };
|
|
return { message: "dashboard event", status: 200, data: record };
|
|
},
|
|
|
|
async agreement(input, userId) {
|
|
if (!isUuid(input?.agreementUuid)) return { message: "invalid request", status: 400, data: { error: "invalid agreement id" } };
|
|
const record = await service.agreement(userId, input.agreementUuid);
|
|
if (!record) return { message: "dashboard agreement", status: 404, data: { error: "agreement not found" } };
|
|
return { message: "dashboard agreement", status: 200, data: record };
|
|
},
|
|
|
|
async details(input, userId) {
|
|
if (!isUuid(input?.eventUuid)) return { message: "invalid request", status: 400, data: { error: "invalid event id" } };
|
|
const record = await service.details(userId, input.eventUuid);
|
|
if(!record) return { message: "dashboard event details", status: 404, data: { error: "event details not found" } };
|
|
return { message: "dashboard event details", status: 200, data: record };
|
|
},
|
|
};
|
|
}
|
|
|
|
export const dashboardApi = makeDashboardApi();
|