44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { AppError } from '../common/errors/app-error';
|
|
import type { DatabaseService } from '../database/database.service';
|
|
import type {
|
|
Prisma,
|
|
B2bPartnerOrder,
|
|
B2bPartnerOrderLine,
|
|
B2bPartnerOrderCancellation,
|
|
} from '../generated/prisma/client';
|
|
|
|
type Db = DatabaseService | Prisma.TransactionClient;
|
|
export const partnerOrderInclude = {
|
|
lines: { orderBy: { id: 'asc' as const } },
|
|
cancellation: true,
|
|
};
|
|
export async function requirePartnerOrder(
|
|
db: Db,
|
|
organizationId: string,
|
|
id: string,
|
|
) {
|
|
const order = await db.b2bPartnerOrder.findFirst({
|
|
where: { id, organizationId },
|
|
include: partnerOrderInclude,
|
|
});
|
|
if (!order) throw new AppError('B2B_ORDER_NOT_FOUND');
|
|
return order;
|
|
}
|
|
export function presentPartnerOrder(
|
|
order: B2bPartnerOrder & {
|
|
lines: B2bPartnerOrderLine[];
|
|
cancellation: B2bPartnerOrderCancellation | null;
|
|
},
|
|
) {
|
|
return {
|
|
...order,
|
|
status: order.cancellation ? 'CANCELLED' : 'RECEIVED',
|
|
merchandiseTotal: order.merchandiseTotal.toFixed(2),
|
|
lines: order.lines.map((line) => ({
|
|
...line,
|
|
unitPrice: line.unitPrice.toFixed(2),
|
|
lineTotal: line.lineTotal.toFixed(2),
|
|
})),
|
|
};
|
|
}
|