Initial admin edits
This commit is contained in:
@@ -35,6 +35,12 @@ export class CompaniesController {
|
||||
return this.companiesService.findAll(true);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Get('payment/bank-accounts')
|
||||
listPaymentBankAccounts() {
|
||||
return this.companiesService.listPaymentBankAccounts();
|
||||
}
|
||||
|
||||
@AuthType('any')
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
ConflictException,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
isPaymentRecipientCompany,
|
||||
requirePaymentRecipientCompany,
|
||||
} from './default-company';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { resolveMaxMeterage } from '../common/utils/meterage';
|
||||
@@ -146,8 +151,21 @@ export class CompaniesService {
|
||||
return this.mapCompany(company);
|
||||
}
|
||||
|
||||
/** حسابهای واریز پرداخت مشتری — همیشه نادمی تکس */
|
||||
async listPaymentBankAccounts() {
|
||||
const recipient = await requirePaymentRecipientCompany(this.prisma);
|
||||
return this.listBankAccountsForCompany(recipient.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* لیست حساب برای اپ/ادمین: پرداخت همیشه به نادمی تکس است، نه شرکت مالک محصول.
|
||||
*/
|
||||
async listBankAccounts(companyId: string) {
|
||||
await this.ensureCompany(companyId);
|
||||
return this.listPaymentBankAccounts();
|
||||
}
|
||||
|
||||
private async listBankAccountsForCompany(companyId: string) {
|
||||
const accounts = await this.prisma.bankAccount.findMany({
|
||||
where: { companyId },
|
||||
orderBy: { displayOrder: 'asc' },
|
||||
@@ -156,10 +174,16 @@ export class CompaniesService {
|
||||
}
|
||||
|
||||
async createBankAccount(companyId: string, dto: CreateBankAccountDto) {
|
||||
await this.ensureCompany(companyId);
|
||||
const company = await this.ensureCompany(companyId);
|
||||
if (!isPaymentRecipientCompany(company)) {
|
||||
throw new BadRequestException(
|
||||
'حساب بانکی فقط برای شرکت نادمی تکس (دریافتکننده پرداخت) ثبت میشود.',
|
||||
);
|
||||
}
|
||||
const recipient = await requirePaymentRecipientCompany(this.prisma);
|
||||
const account = await this.prisma.bankAccount.create({
|
||||
data: {
|
||||
companyId,
|
||||
companyId: recipient.id,
|
||||
bankName: dto.bankName.trim(),
|
||||
accountHolder: dto.accountHolder.trim(),
|
||||
accountNumber: dto.accountNumber.trim(),
|
||||
@@ -234,6 +258,7 @@ export class CompaniesService {
|
||||
id: company.id,
|
||||
name: company.name,
|
||||
slug: company.slug,
|
||||
isPaymentRecipient: isPaymentRecipientCompany(company),
|
||||
description: company.description ?? '',
|
||||
logoUrl: company.logoUrl,
|
||||
logo: company.logoUrl ?? undefined,
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import {
|
||||
PAYMENT_RECIPIENT_COMPANY_SLUG,
|
||||
isPaymentRecipientCompany,
|
||||
} from './default-company';
|
||||
|
||||
describe('default-company', () => {
|
||||
it('uses stable slug for payment recipient', () => {
|
||||
expect(PAYMENT_RECIPIENT_COMPANY_SLUG).toBe('nademitex');
|
||||
});
|
||||
|
||||
it('detects payment recipient by slug', () => {
|
||||
expect(isPaymentRecipientCompany({ slug: 'nademitex' })).toBe(true);
|
||||
expect(isPaymentRecipientCompany({ slug: 'par' })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import type { Company, PrismaClient } from '@prisma/client';
|
||||
|
||||
/** Slug ثابت شرکت اصلی — منبع حقیقت برای دریافت پرداخت و پیشفرض محصول */
|
||||
export const PAYMENT_RECIPIENT_COMPANY_SLUG = 'nademitex';
|
||||
|
||||
type PrismaLike = Pick<PrismaClient, 'company'>;
|
||||
|
||||
export async function findPaymentRecipientCompany(
|
||||
prisma: PrismaLike,
|
||||
): Promise<Company | null> {
|
||||
return prisma.company.findFirst({
|
||||
where: { slug: PAYMENT_RECIPIENT_COMPANY_SLUG, isActive: true },
|
||||
});
|
||||
}
|
||||
|
||||
export async function requirePaymentRecipientCompany(
|
||||
prisma: PrismaLike,
|
||||
): Promise<Company> {
|
||||
const company = await findPaymentRecipientCompany(prisma);
|
||||
if (!company) {
|
||||
throw new NotFoundException(
|
||||
'شرکت اصلی (نادمی تکس) در سیستم تعریف نشده است. لطفاً با پشتیبانی تماس بگیرید.',
|
||||
);
|
||||
}
|
||||
return company;
|
||||
}
|
||||
|
||||
/** شرکت مرتبط با محصول؛ در صورت نبود مقدار معتبر، نادمی تکس */
|
||||
export async function resolveProductCompanyId(
|
||||
prisma: PrismaLike,
|
||||
companyId?: string | null,
|
||||
): Promise<string> {
|
||||
const trimmed = companyId?.trim();
|
||||
if (trimmed) {
|
||||
const row = await prisma.company.findUnique({ where: { id: trimmed } });
|
||||
if (!row) {
|
||||
throw new BadRequestException('شرکت انتخابشده معتبر نیست');
|
||||
}
|
||||
if (!row.isActive) {
|
||||
throw new BadRequestException('شرکت انتخابشده غیرفعال است');
|
||||
}
|
||||
return row.id;
|
||||
}
|
||||
return (await requirePaymentRecipientCompany(prisma)).id;
|
||||
}
|
||||
|
||||
export function isPaymentRecipientCompany(company: {
|
||||
slug: string;
|
||||
}): boolean {
|
||||
return company.slug === PAYMENT_RECIPIENT_COMPANY_SLUG;
|
||||
}
|
||||
Reference in New Issue
Block a user