Restructuring calculator data in database and improve api surface
This commit is contained in:
+49
-24
@@ -11,7 +11,7 @@ import {
|
||||
Webflow,
|
||||
} from "@blade-and-brawn/commerce";
|
||||
import zipcodesUs from "zipcodes-us";
|
||||
import { env, log } from "./util";
|
||||
import { DEFAULT_NAME, env, log } from "./util";
|
||||
import serverTiming from "@elysia/server-timing";
|
||||
import jwt from "@elysia/jwt";
|
||||
import { CommerceService } from "./services/commerce";
|
||||
@@ -19,13 +19,16 @@ import cluster from "node:cluster";
|
||||
import { randomUUIDv7 } from "bun";
|
||||
import { CalculatorService, CalculatorUnavailableError } from "./services/calculator";
|
||||
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
||||
import { StandardsService } from "./services/standards";
|
||||
|
||||
// SERVICES
|
||||
// -----------
|
||||
const s = {
|
||||
Calculator: new CalculatorService(),
|
||||
Commerce: new CommerceService()
|
||||
};
|
||||
const s = (() => {
|
||||
const Standards = new StandardsService();
|
||||
const Calculator = new CalculatorService(DEFAULT_NAME);
|
||||
const Commerce = new CommerceService();
|
||||
return { Standards, Calculator, Commerce };
|
||||
})();
|
||||
|
||||
// ELYSIA
|
||||
// -----------
|
||||
@@ -47,7 +50,7 @@ export const app = new Elysia()
|
||||
.error({
|
||||
PrintfulError,
|
||||
WebflowError,
|
||||
CalculatorUnavailableError,
|
||||
CalculatorUnavailableError
|
||||
})
|
||||
|
||||
.onError(({ code, error }) => {
|
||||
@@ -115,31 +118,53 @@ export const app = new Elysia()
|
||||
activityPerformances: t.Array(ActivityPerformanceSchema)
|
||||
}),
|
||||
})
|
||||
// Authenticated
|
||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||
const token = auth.value && await jwt.verify(auth.value);
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
.put("/config/:id", async ({ params: { id }, body: { datasetId, params: parameters } }) => {
|
||||
await s.Calculator.Standards.Config.update(id, datasetId, parameters);
|
||||
}, {
|
||||
params: t.Object({ id: t.String() }),
|
||||
body: t.Object({ datasetId: t.String(), params: StandardsParamsSchema })
|
||||
})
|
||||
.post("/config", async ({ body: { datasetId, params } }) => {
|
||||
await s.Calculator.Standards.Config.create(datasetId, params);
|
||||
}, {
|
||||
body: t.Object({ datasetId: t.String(), params: StandardsParamsSchema })
|
||||
})
|
||||
.post("/config/:id/switch", async ({ params: { id } }) => {
|
||||
await s.Calculator.Standards.Config.switch(id);
|
||||
}, {
|
||||
params: t.Object({ id: t.String() })
|
||||
})
|
||||
.get("/config", async () => {
|
||||
.get("/standards/config", async () => {
|
||||
return await s.Calculator.Standards.Config.get();
|
||||
})
|
||||
.post("/standards/config/switch", async ({ body: { standardsConfigId: id } }) => {
|
||||
await s.Calculator.Standards.Config.switch(id);
|
||||
}, {
|
||||
body: t.Object({ standardsConfigId: t.String() })
|
||||
})
|
||||
)
|
||||
.group("/standards", (app) => app
|
||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||
const token = auth.value && await jwt.verify(auth.value);
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
.post("/configs", async ({ body: { name, datasetId, params } }) => {
|
||||
return await s.Standards.Configs.create(name, datasetId, params);
|
||||
}, {
|
||||
body: t.Object({ name: t.String(), datasetId: t.String(), params: StandardsParamsSchema })
|
||||
})
|
||||
.get("/configs", async () => {
|
||||
return await s.Standards.Configs.list();
|
||||
})
|
||||
.get("/configs/:id", async ({ params: { id } }) => {
|
||||
return await s.Standards.Configs.get(id);
|
||||
}, {
|
||||
params: t.Object({ id: t.String() })
|
||||
})
|
||||
.put("/configs/:id", async ({ params: { id }, body: { name, datasetId, params: parameters } }) => {
|
||||
await s.Standards.Configs.update(id, name, datasetId, parameters);
|
||||
}, {
|
||||
params: t.Object({ id: t.String() }),
|
||||
body: t.Object({ name: t.String(), datasetId: t.String(), params: StandardsParamsSchema })
|
||||
})
|
||||
.get("/datasets", async () => {
|
||||
return await s.Standards.Datasets.list();
|
||||
})
|
||||
.get("/datasets/:id", async ({ params: { id } }) => {
|
||||
return await s.Standards.Datasets.get(id);
|
||||
}, {
|
||||
params: t.Object({ id: t.String() })
|
||||
})
|
||||
)
|
||||
|
||||
// COMMERCE
|
||||
|
||||
Reference in New Issue
Block a user