Initialize the API and add managed app banners.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-09 17:08:47 +03:30
co-authored by Cursor
commit b15fefe438
158 changed files with 25624 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
import { Body, Controller, Get, Param, Patch, Query } from '@nestjs/common';
import { ChecksService } from './checks.service';
import { AuthType, Roles } from '../common/decorators/auth.decorators';
import { CurrentUser } from '../common/decorators/current-user.decorator';
import type { AuthPrincipal } from '../common/decorators/current-user.decorator';
import { UpdateCheckStatusDto } from './dto/check.dto';
@Controller()
@AuthType('admin')
@Roles('admin', 'finance')
export class ChecksController {
constructor(private readonly checksService: ChecksService) {}
@Get('admin/checks')
findAll(@Query('status') status?: string) {
return this.checksService.findAll(status);
}
@Get('admin/orders/:orderId/checks')
findByOrder(@Param('orderId') orderId: string) {
return this.checksService.findByOrder(orderId);
}
@Patch('admin/checks/:id/status')
updateStatus(
@CurrentUser() user: AuthPrincipal,
@Param('id') id: string,
@Body() dto: UpdateCheckStatusDto,
) {
return this.checksService.updateStatus(id, user.id, dto);
}
}
+12
View File
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { ChecksController } from './checks.controller';
import { ChecksService } from './checks.service';
import { NotificationsModule } from '../notifications/notifications.module';
@Module({
imports: [NotificationsModule],
controllers: [ChecksController],
providers: [ChecksService],
exports: [ChecksService],
})
export class ChecksModule {}
+164
View File
@@ -0,0 +1,164 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { CheckStatus, OrderStatus } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { UpdateCheckStatusDto } from './dto/check.dto';
import { NotificationsService } from '../notifications/notifications.service';
import { joinFullName } from '../common/utils/name';
@Injectable()
export class ChecksService {
constructor(
private readonly prisma: PrismaService,
private readonly notifications: NotificationsService,
) {}
async findByOrder(orderId: string) {
const order = await this.prisma.order.findUnique({
where: { id: orderId },
});
if (!order) throw new NotFoundException('سفارش یافت نشد');
const checks = await this.prisma.check.findMany({
where: { orderId },
orderBy: { row: 'asc' },
});
return checks.map((c) => this.map(c));
}
async findAll(status?: string) {
const where =
status && Object.values(CheckStatus).includes(status as CheckStatus)
? { status: status as CheckStatus }
: {};
const checks = await this.prisma.check.findMany({
where,
include: {
order: {
include: { user: true },
},
},
orderBy: [{ dueDate: 'asc' }, { createdAt: 'desc' }],
});
return checks.map((c) => ({
...this.map(c),
orderNumber: c.order.orderNumber,
customerName: joinFullName(c.order.user.firstName, c.order.user.lastName),
orderStatus: c.order.status,
settlementType: c.order.settlementType,
}));
}
async updateStatus(id: string, adminId: string, dto: UpdateCheckStatusDto) {
const check = await this.prisma.check.findUnique({
where: { id },
include: { order: { include: { checks: true } } },
});
if (!check) throw new NotFoundException('چک یافت نشد');
let notifyStatus: OrderStatus | null = null;
const updated = await this.prisma.$transaction(async (tx) => {
const result = await tx.check.update({
where: { id },
data: {
status: dto.status,
notes: dto.note
? [check.notes, dto.note].filter(Boolean).join('\n')
: check.notes,
},
});
const siblings = await tx.check.findMany({
where: { orderId: check.orderId },
});
const allApproved = siblings.every(
(c) => c.status === CheckStatus.approved,
);
const anyRejected = siblings.some(
(c) => c.status === CheckStatus.rejected,
);
// رد چک باید قابل اصلاح بماند؛ check_rejected راه برگشتی برای مشتری ندارد.
let nextStatus: OrderStatus | null = null;
if (anyRejected) {
nextStatus = OrderStatus.check_needs_fix;
} else if (allApproved) {
nextStatus = OrderStatus.checks_approved;
}
if (nextStatus && nextStatus !== check.order.status) {
await tx.order.update({
where: { id: check.orderId },
data: { status: nextStatus },
});
await tx.orderStatusHistory.create({
data: {
orderId: check.orderId,
fromStatus: check.order.status,
toStatus: nextStatus,
changedBy: `admin:${adminId}`,
note: dto.note || `وضعیت چک به ${dto.status} تغییر کرد`,
},
});
notifyStatus = nextStatus;
}
return result;
});
if (notifyStatus === OrderStatus.checks_approved) {
this.notifications.dispatchOrderEvent(check.orderId, 'checks_approved');
} else if (notifyStatus === OrderStatus.check_needs_fix) {
this.notifications.dispatchOrderEvent(check.orderId, 'checks_need_fix', {
note: dto.note ?? null,
});
}
return this.map(updated);
}
private map(c: {
id: string;
orderId: string;
row: number;
amount: number;
dueDate: Date;
sayadId: string | null;
checkNumber: string | null;
ownerNationalId: string | null;
ownerFirstName: string | null;
ownerLastName: string | null;
bankName: string | null;
branchName: string | null;
frontImage: string | null;
backImage: string | null;
notes: string | null;
mode: string;
status: CheckStatus;
createdAt: Date;
updatedAt: Date;
}) {
return {
id: c.id,
orderId: c.orderId,
row: c.row,
amount: c.amount,
dueDate: c.dueDate.toISOString(),
sayadId: c.sayadId ?? '',
checkNumber: c.checkNumber ?? undefined,
ownerNationalId: c.ownerNationalId ?? undefined,
ownerFirstName: c.ownerFirstName ?? undefined,
ownerLastName: c.ownerLastName ?? undefined,
bankName: c.bankName ?? '',
branchName: c.branchName ?? undefined,
frontImage: c.frontImage ?? undefined,
backImage: c.backImage ?? undefined,
notes: c.notes ?? undefined,
mode: c.mode,
status: c.status,
createdAt: c.createdAt.toISOString(),
updatedAt: c.updatedAt.toISOString(),
};
}
}
+11
View File
@@ -0,0 +1,11 @@
import { IsEnum, IsOptional, IsString } from 'class-validator';
import { CheckStatus } from '@prisma/client';
export class UpdateCheckStatusDto {
@IsEnum(CheckStatus)
status!: CheckStatus;
@IsOptional()
@IsString()
note?: string;
}