Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions services/api/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
offlineLicenses,
entitlementEvents,
entitlements,
episodes,
feedback,
multiplayerBans,
multiplayerBlocks,
Expand All @@ -21,7 +22,7 @@ import {
users,
} from "@nightcell7/database";
import type { AccountContext } from "@nightcell7/auth";
import type { OrderStatus } from "@nightcell7/entitlements";
import { findCatalogEntry, type OrderStatus } from "@nightcell7/entitlements";

/**
* Data access for the API service.
Expand Down Expand Up @@ -263,24 +264,43 @@ export function createRepositories(db: Database): Repositories {
},

async createOrder(input) {
await db.insert(orders).values({
id: input.id,
userId: input.userId,
email: input.email,
provider: "coinpay",
currency: input.currency,
subtotal: input.unitAmount,
tax: 0,
total: input.unitAmount,
status: "created",
idempotencyKey: input.idempotencyKey,
});
await db.insert(orderItems).values({
id: `oi_${input.id}`,
orderId: input.id,
episodeId: input.episodeId,
unitAmount: input.unitAmount,
quantity: 1,
const entry = findCatalogEntry(input.episodeId);
await db.transaction(async (tx) => {
// order_items (and entitlements) reference episodes(id), and nothing
// seeds that table: the server catalog is the authority, so the row
// it needs is written from the catalog on first sale. Without it every
// checkout failed on the foreign key.
if (entry) {
await tx
.insert(episodes)
.values({
id: entry.episodeId,
slug: entry.episodeId,
title: entry.title,
theater: "NIGHTCELL 7",
status: entry.status,
})
.onConflictDoNothing();
}
await tx.insert(orders).values({
id: input.id,
userId: input.userId,
email: input.email,
provider: "coinpay",
currency: input.currency,
subtotal: input.unitAmount,
tax: 0,
total: input.unitAmount,
status: "created",
idempotencyKey: input.idempotencyKey,
});
await tx.insert(orderItems).values({
id: `oi_${input.id}`,
orderId: input.id,
episodeId: input.episodeId,
unitAmount: input.unitAmount,
quantity: 1,
});
});
},

Expand Down
Loading