diff --git a/src/collections/users/Users.ts b/src/collections/users/Users.ts index a8b19ff..ace8ec8 100644 --- a/src/collections/users/Users.ts +++ b/src/collections/users/Users.ts @@ -17,7 +17,7 @@ export const Users: CollectionConfig = { const userId = doc.id as number; try { - await ensurePersonalAccount(payload, userId); + await ensurePersonalAccount(payload, userId, req); } catch (e) { payload.logger.error( `[Users] Failed to create personal bank account for user ${userId}: ${e}`, @@ -37,6 +37,7 @@ export const Users: CollectionConfig = { limit: 1, depth: 0, overrideAccess: true, + req, }); recruit = (ranks as { docs: Array<{ id: number }> }).docs[0]; @@ -49,6 +50,7 @@ export const Users: CollectionConfig = { description: "Entry-level rank for new members.", }, overrideAccess: true, + req, }); recruit = { id: created.id }; payload.logger.info("[Users] Created default 'Recruit' rank on demand."); @@ -74,6 +76,7 @@ export const Users: CollectionConfig = { } as any, }, overrideAccess: true, + req, }); } catch (e) { payload.logger.error(`[Users] Failed to create profile for user ${userId}: ${e}`); diff --git a/src/lib/banking/index.ts b/src/lib/banking/index.ts index 6b3e4ef..9a567af 100644 --- a/src/lib/banking/index.ts +++ b/src/lib/banking/index.ts @@ -1,3 +1,4 @@ +import type { PayloadRequest } from "payload"; import type { BankAccount, BankTransaction } from "@/payload-types"; export type BankAccountType = "treasury" | "faction" | "personal"; @@ -70,6 +71,11 @@ export async function createAccount( ownerFactionId?: number; ownerUserId?: number; }, + // Optional request carrying a transactionID. Pass it when calling from + // inside another operation's hooks so nested writes join that transaction — + // on a fresh connection the uncommitted parent row is invisible and FK + // checks fail. + req?: PayloadRequest, ): Promise { const { name, accountType, ownerFactionId, ownerUserId } = input; const currencyId = await getMainCurrencyId(payload); @@ -89,12 +95,14 @@ export async function createAccount( }, overrideAccess: true, depth: 0, + req, })) as unknown as BankAccount; } export async function ensurePersonalAccount( payload: PayloadType, userId: number, + req?: PayloadRequest, ): Promise { const existing = (await payload.find({ collection: "bank-accounts", @@ -104,6 +112,7 @@ export async function ensurePersonalAccount( limit: 1, depth: 0, overrideAccess: true, + req, })) as unknown as { docs: BankAccount[] }; if (existing.docs.length > 0) return existing.docs[0]; @@ -113,15 +122,20 @@ export async function ensurePersonalAccount( id: userId, depth: 0, overrideAccess: true, + req, }) .catch(() => null)) as unknown as { username?: string; displayName?: string } | null; const label = user?.displayName || user?.username || `User ${userId}`; - return createAccount(payload, { - name: `${label}'s Account`, - accountType: "personal", - ownerUserId: userId, - }); + return createAccount( + payload, + { + name: `${label}'s Account`, + accountType: "personal", + ownerUserId: userId, + }, + req, + ); } export async function applyTransaction(