131 lines
3.9 KiB
TypeScript
131 lines
3.9 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { AppError } from '../common/errors/app-error';
|
|
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 {
|
|
assertEligiblePriceVariants,
|
|
requireBusinessAccount,
|
|
requirePriceList,
|
|
} from './price-list.helpers';
|
|
import type {
|
|
CreatePriceListInput,
|
|
PriceListQuery,
|
|
} from './price-list.schemas';
|
|
|
|
@Injectable()
|
|
export class PriceListStore {
|
|
constructor(
|
|
private readonly db: DatabaseService,
|
|
private readonly access: AccessStore,
|
|
) {}
|
|
|
|
async list(actor: Principal, accountId: string, query: PriceListQuery) {
|
|
await requireBusinessAccount(this.db, actor.organizationId, accountId);
|
|
return this.db.b2bPriceList.findMany({
|
|
where: {
|
|
organizationId: actor.organizationId,
|
|
accountId,
|
|
...(query.status ? { status: query.status } : {}),
|
|
},
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: query.limit,
|
|
skip: query.offset,
|
|
});
|
|
}
|
|
|
|
get(actor: Principal, id: string) {
|
|
return requirePriceList(this.db, actor.organizationId, id);
|
|
}
|
|
|
|
create(actor: Principal, accountId: string, input: CreatePriceListInput) {
|
|
return this.access.mutate(actor, 'b2b.price.manage', async (tx) => {
|
|
await requireBusinessAccount(tx, actor.organizationId, accountId);
|
|
const list = await tx.b2bPriceList.create({
|
|
data: {
|
|
...input,
|
|
organizationId: actor.organizationId,
|
|
accountId,
|
|
createdById: actor.userId,
|
|
},
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'b2b_price_list.created',
|
|
list.id,
|
|
);
|
|
return list;
|
|
});
|
|
}
|
|
|
|
activate(actor: Principal, id: string) {
|
|
return this.access.mutate(actor, 'b2b.price.manage', async (tx) => {
|
|
const list = await requirePriceList(tx, actor.organizationId, id);
|
|
if (list.status !== 'DRAFT')
|
|
throw new AppError('B2B_PRICE_LIST_DRAFT_REQUIRED');
|
|
const latest = await tx.businessVerificationEvent.findFirst({
|
|
where: {
|
|
accountId: list.accountId,
|
|
organizationId: actor.organizationId,
|
|
},
|
|
orderBy: { sequence: 'desc' },
|
|
});
|
|
if (latest?.kind !== 'APPROVED')
|
|
throw new AppError('B2B_PRICE_ACCOUNT_UNVERIFIED');
|
|
if (!list.items.length) throw new AppError('B2B_PRICE_LIST_EMPTY');
|
|
await assertEligiblePriceVariants(
|
|
tx,
|
|
actor.organizationId,
|
|
list.currency,
|
|
list.items,
|
|
);
|
|
if (
|
|
await tx.b2bPriceList.findFirst({
|
|
where: {
|
|
organizationId: actor.organizationId,
|
|
accountId: list.accountId,
|
|
currency: list.currency,
|
|
status: 'ACTIVE',
|
|
},
|
|
})
|
|
)
|
|
throw new AppError('B2B_PRICE_LIST_ALREADY_ACTIVE');
|
|
const active = await tx.b2bPriceList.update({
|
|
where: { id },
|
|
data: { status: 'ACTIVE', activeSlot: true },
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'b2b_price_list.activated',
|
|
id,
|
|
);
|
|
return active;
|
|
});
|
|
}
|
|
|
|
archive(actor: Principal, id: string) {
|
|
return this.access.mutate(actor, 'b2b.price.manage', async (tx) => {
|
|
const list = await requirePriceList(tx, actor.organizationId, id);
|
|
if (list.status !== 'ACTIVE')
|
|
throw new AppError('B2B_PRICE_LIST_ACTIVE_REQUIRED');
|
|
const archived = await tx.b2bPriceList.update({
|
|
where: { id },
|
|
data: { status: 'ARCHIVED', activeSlot: null },
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'b2b_price_list.archived',
|
|
id,
|
|
);
|
|
return archived;
|
|
});
|
|
}
|
|
}
|