103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import { commandHash } from '../common/idempotency';
|
|
import { DatabaseService } from '../database/database.service';
|
|
import { AccessStore } from '../identity/access.store';
|
|
import { recordAudit } from '../identity/audit';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import { isVerifiedBusinessAccount } from './business-verification';
|
|
import { requireBusinessAccount } from './price-list.helpers';
|
|
import {
|
|
customCandleInclude,
|
|
customCandleView,
|
|
requireCustomCandle,
|
|
} from './custom-candle-lookup';
|
|
import type {
|
|
CustomCandleQuery,
|
|
CustomCandleRequestInput,
|
|
} from './custom-candle.schemas';
|
|
|
|
@Injectable()
|
|
export class CustomCandleStore {
|
|
constructor(
|
|
private readonly db: DatabaseService,
|
|
private readonly access: AccessStore,
|
|
) {}
|
|
|
|
async list(actor: Principal, query: CustomCandleQuery) {
|
|
const requests = await this.db.b2bCustomCandleRequest.findMany({
|
|
where: {
|
|
organizationId: actor.organizationId,
|
|
...(query.accountId ? { accountId: query.accountId } : {}),
|
|
...(query.status
|
|
? {
|
|
decision:
|
|
query.status === 'REQUESTED'
|
|
? { is: null }
|
|
: { is: { kind: query.status } },
|
|
}
|
|
: {}),
|
|
},
|
|
include: customCandleInclude,
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: query.limit,
|
|
skip: query.offset,
|
|
});
|
|
return requests.map(customCandleView);
|
|
}
|
|
|
|
async get(actor: Principal, id: string) {
|
|
return customCandleView(
|
|
await requireCustomCandle(this.db, actor.organizationId, id),
|
|
);
|
|
}
|
|
|
|
request(actor: Principal, input: CustomCandleRequestInput) {
|
|
const { idempotencyKey, ...terms } = input;
|
|
const hash = commandHash(JSON.stringify(terms));
|
|
return this.access.mutate(actor, 'b2b.custom.request', async (tx) => {
|
|
const replay = await tx.b2bCustomCandleRequest.findUnique({
|
|
where: {
|
|
organizationId_idempotencyKey: {
|
|
organizationId: actor.organizationId,
|
|
idempotencyKey,
|
|
},
|
|
},
|
|
include: customCandleInclude,
|
|
});
|
|
if (replay) {
|
|
if (replay.requestHash !== hash)
|
|
throw new AppError('B2B_CUSTOM_CANDLE_REQUEST_REPLAY_CONFLICT');
|
|
return customCandleView(replay);
|
|
}
|
|
await requireBusinessAccount(tx, actor.organizationId, input.accountId);
|
|
if (
|
|
!(await isVerifiedBusinessAccount(
|
|
tx,
|
|
actor.organizationId,
|
|
input.accountId,
|
|
))
|
|
)
|
|
throw new AppError('B2B_CUSTOM_CANDLE_ACCOUNT_UNVERIFIED');
|
|
const request = await tx.b2bCustomCandleRequest.create({
|
|
data: {
|
|
...terms,
|
|
organizationId: actor.organizationId,
|
|
requestedById: actor.userId,
|
|
idempotencyKey,
|
|
requestHash: hash,
|
|
},
|
|
include: customCandleInclude,
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'b2b_custom_candle.requested',
|
|
request.id,
|
|
);
|
|
return customCandleView(request);
|
|
});
|
|
}
|
|
}
|