Let AI go to work
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
export * from './items.js';
|
||||
export * from './orders.js';
|
||||
export * from "./items.js";
|
||||
export * from "./orders.js";
|
||||
|
||||
@@ -1,73 +1,80 @@
|
||||
import type { Debugger as Debug } from 'debug';
|
||||
import type { RouteOptions, FastifyRequest, FastifyReply } from 'fastify';
|
||||
import type { Engine } from '@xo-cash/engine'
|
||||
import type { Database } from '../services/database/database.js'
|
||||
import type { Debugger as Debug } from "debug";
|
||||
import type { RouteOptions, FastifyRequest, FastifyReply } from "fastify";
|
||||
import type { Engine } from "@xo-cash/engine";
|
||||
import type { Database } from "../services/database/database.js";
|
||||
|
||||
import { z } from 'zod';
|
||||
import { z } from "zod";
|
||||
|
||||
export type ItemsRouteDeps = {
|
||||
database: Database;
|
||||
engine: Engine;
|
||||
debug: Debug;
|
||||
}
|
||||
};
|
||||
|
||||
export class ItemsRoute {
|
||||
public constructor(private readonly deps: ItemsRouteDeps) {}
|
||||
public constructor(private readonly deps: ItemsRouteDeps) {}
|
||||
|
||||
public async getRoutes(): Promise<Array<RouteOptions>> {
|
||||
return [
|
||||
{
|
||||
method: 'GET',
|
||||
url: '/items',
|
||||
handler: this.getItems.bind(this),
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
url: '/items/:id',
|
||||
handler: this.getItem.bind(this),
|
||||
},
|
||||
]
|
||||
public async getRoutes(): Promise<Array<RouteOptions>> {
|
||||
return [
|
||||
{
|
||||
method: "GET",
|
||||
url: "/items",
|
||||
handler: this.getItems.bind(this),
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
url: "/items/:id",
|
||||
handler: this.getItem.bind(this),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all items from the database
|
||||
* @param request
|
||||
* @param reply
|
||||
* @returns
|
||||
*/
|
||||
private async getItems(request: FastifyRequest, reply: FastifyReply) {
|
||||
// Get all items from the database.
|
||||
const items = await this.deps.database.db
|
||||
.selectFrom("items")
|
||||
.selectAll()
|
||||
.execute();
|
||||
|
||||
// Return the items.
|
||||
return reply.send(items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from the database by id
|
||||
* @param request
|
||||
* @param reply
|
||||
* @returns
|
||||
*/
|
||||
private async getItem(request: FastifyRequest, reply: FastifyReply) {
|
||||
// Parse the request parameters.
|
||||
const { id } = ItemsRoute.getItemSchema.parse(request.params);
|
||||
|
||||
// Get the item from the database.
|
||||
const item = await this.deps.database.db
|
||||
.selectFrom("items")
|
||||
.where("id", "=", id)
|
||||
.selectAll()
|
||||
.executeTakeFirst();
|
||||
|
||||
// If the item is not found, return a 404 error.
|
||||
if (!item) {
|
||||
return reply.status(404).send({
|
||||
error: "Item not found",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all items from the database
|
||||
* @param request
|
||||
* @param reply
|
||||
* @returns
|
||||
*/
|
||||
private async getItems(request: FastifyRequest, reply: FastifyReply) {
|
||||
// Get all items from the database.
|
||||
const items = await this.deps.database.db.selectFrom('items').selectAll().execute();
|
||||
// Return the item.
|
||||
return reply.send(item);
|
||||
}
|
||||
|
||||
// Return the items.
|
||||
return reply.send(items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from the database by id
|
||||
* @param request
|
||||
* @param reply
|
||||
* @returns
|
||||
*/
|
||||
private async getItem(request: FastifyRequest, reply: FastifyReply) {
|
||||
// Parse the request parameters.
|
||||
const { id } = ItemsRoute.getItemSchema.parse(request.params);
|
||||
|
||||
// Get the item from the database.
|
||||
const item = await this.deps.database.db.selectFrom('items').where('id', '=', id).selectAll().executeTakeFirst();
|
||||
|
||||
// If the item is not found, return a 404 error.
|
||||
if (!item) {
|
||||
return reply.status(404).send({
|
||||
error: 'Item not found'
|
||||
});
|
||||
}
|
||||
|
||||
// Return the item.
|
||||
return reply.send(item);
|
||||
}
|
||||
|
||||
static getItemSchema = z.object({
|
||||
id: z.string(),
|
||||
});
|
||||
}
|
||||
static getItemSchema = z.object({
|
||||
id: z.string(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,88 +1,107 @@
|
||||
import type { Debugger as Debug } from 'debug';
|
||||
import type { RouteOptions, FastifyRequest, FastifyReply } from 'fastify';
|
||||
import type { Engine } from '@xo-cash/engine'
|
||||
import type { Database } from '../services/database/database.js'
|
||||
import type { Debugger as Debug } from "debug";
|
||||
import type { RouteOptions, FastifyRequest, FastifyReply } from "fastify";
|
||||
|
||||
import { z } from 'zod';
|
||||
import { z } from "zod";
|
||||
|
||||
import type { Database } from "../services/database/database.js";
|
||||
import {
|
||||
OrderPaymentError,
|
||||
OrderPaymentService,
|
||||
} from "../services/order-payment-service.js";
|
||||
|
||||
export type OrdersRouteDeps = {
|
||||
database: Database;
|
||||
engine: Engine
|
||||
orderPaymentService: OrderPaymentService;
|
||||
syncServerUrl: string;
|
||||
debug: Debug;
|
||||
}
|
||||
};
|
||||
|
||||
export class OrdersRoute {
|
||||
public constructor(private readonly deps: OrdersRouteDeps) {}
|
||||
public constructor(private readonly deps: OrdersRouteDeps) {}
|
||||
|
||||
public async getRoutes(): Promise<Array<RouteOptions>> {
|
||||
return [
|
||||
{
|
||||
method: 'GET',
|
||||
url: '/orders',
|
||||
handler: this.getOrders.bind(this),
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
url: '/orders',
|
||||
handler: this.createOrder.bind(this),
|
||||
},
|
||||
]
|
||||
public async getRoutes(): Promise<Array<RouteOptions>> {
|
||||
return [
|
||||
{
|
||||
method: "GET",
|
||||
url: "/orders",
|
||||
handler: this.getOrders.bind(this),
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
url: "/orders/:id",
|
||||
handler: this.getOrder.bind(this),
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
url: "/orders",
|
||||
handler: this.createOrder.bind(this),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
private async getOrders(_request: FastifyRequest, reply: FastifyReply) {
|
||||
const orders = await this.deps.database.db
|
||||
.selectFrom("orders")
|
||||
.selectAll()
|
||||
.execute();
|
||||
|
||||
return reply.send(
|
||||
orders.map((order) => ({
|
||||
...order,
|
||||
items: JSON.parse(order.items),
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
private async getOrder(request: FastifyRequest, reply: FastifyReply) {
|
||||
const { id } = OrdersRoute.getOrderSchema.parse(request.params);
|
||||
|
||||
const order = await this.deps.database.db
|
||||
.selectFrom("orders")
|
||||
.selectAll()
|
||||
.where("id", "=", id)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!order) {
|
||||
return reply.status(404).send({ error: "Order not found" });
|
||||
}
|
||||
|
||||
private async getOrders(request: FastifyRequest, reply: FastifyReply) {
|
||||
// Get all orders from the database.
|
||||
const orders = await this.deps.database.db.selectFrom('orders').selectAll().execute();
|
||||
|
||||
// Return the orders.
|
||||
return reply.send(orders);
|
||||
}
|
||||
|
||||
private async createOrder(request: FastifyRequest, reply: FastifyReply) {
|
||||
// Parse the request body.
|
||||
const { items: itemsInput } = OrdersRoute.createOrderSchema.parse(request.body);
|
||||
|
||||
// Get the items from the database.
|
||||
const items = await this.deps.database.db.selectFrom('items').where('id', 'in', itemsInput.map((item) => item.id)).selectAll().execute();
|
||||
|
||||
// If the items are not found, return a 404 error.
|
||||
if (items.length !== items.length) {
|
||||
return reply.status(404).send({
|
||||
error: 'Items not found'
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Create an XO Engine Invitation with the relavent data in it so we can pass it back to the client.
|
||||
|
||||
// Create the order in the database.
|
||||
const order = await this.deps.database.db.insertInto('orders').values({
|
||||
// user_id: request.user.id,
|
||||
status: 'pending',
|
||||
total_price: 0,
|
||||
total_quantity: 0,
|
||||
items: JSON.stringify(items.map((item) => ({
|
||||
id: item.id,
|
||||
quantity: item.quantity,
|
||||
}))),
|
||||
}).execute();
|
||||
|
||||
// If the order is not created, return a 500 error.
|
||||
if (!order) {
|
||||
return reply.status(500).send({
|
||||
error: 'Failed to create order'
|
||||
});
|
||||
}
|
||||
|
||||
// Return the order.
|
||||
return reply.send(order);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schema for creating an order.
|
||||
*/
|
||||
static createOrderSchema = z.object({
|
||||
items: z.array(z.object({
|
||||
id: z.string(),
|
||||
quantity: z.number(),
|
||||
})),
|
||||
return reply.send({
|
||||
...order,
|
||||
items: JSON.parse(order.items),
|
||||
syncServerUrl: this.deps.syncServerUrl,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async createOrder(request: FastifyRequest, reply: FastifyReply) {
|
||||
const { items: itemsInput } = OrdersRoute.createOrderSchema.parse(
|
||||
request.body,
|
||||
);
|
||||
|
||||
try {
|
||||
const result =
|
||||
await this.deps.orderPaymentService.createOrder(itemsInput);
|
||||
return reply.status(201).send(result);
|
||||
} catch (error) {
|
||||
if (error instanceof OrderPaymentError) {
|
||||
return reply.status(error.statusCode).send({ error: error.message });
|
||||
}
|
||||
|
||||
this.deps.debug("Failed to create order: %o", error);
|
||||
return reply.status(500).send({ error: "Failed to create order" });
|
||||
}
|
||||
}
|
||||
|
||||
static getOrderSchema = z.object({
|
||||
id: z.string(),
|
||||
});
|
||||
|
||||
static createOrderSchema = z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
quantity: z.number().int().positive(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user