2026-07-23 10:25:32 +03:30
|
|
|
import {
|
2026-07-30 18:19:53 +03:30
|
|
|
BadRequestException,
|
2026-07-23 10:25:32 +03:30
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
2026-08-10 15:29:04 +03:30
|
|
|
ForbiddenException,
|
2026-07-23 10:25:32 +03:30
|
|
|
Get,
|
|
|
|
|
Param,
|
|
|
|
|
Patch,
|
|
|
|
|
Post,
|
|
|
|
|
Query,
|
2026-07-30 18:19:53 +03:30
|
|
|
UploadedFile,
|
2026-07-23 10:25:32 +03:30
|
|
|
UseGuards,
|
2026-07-30 18:19:53 +03:30
|
|
|
UseInterceptors,
|
2026-07-23 10:25:32 +03:30
|
|
|
} from '@nestjs/common';
|
2026-07-30 18:19:53 +03:30
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
2026-07-23 10:25:32 +03:30
|
|
|
import { ProductType } from '@prisma/client';
|
2026-07-30 18:19:53 +03:30
|
|
|
import { diskStorage } from 'multer';
|
|
|
|
|
import { extname } from 'path';
|
2026-07-23 10:25:32 +03:30
|
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
2026-08-10 15:29:04 +03:30
|
|
|
import { PermissionsGuard } from '../auth/permissions.guard';
|
|
|
|
|
import { RequirePermissions } from '../auth/permissions.decorator';
|
2026-07-23 10:25:32 +03:30
|
|
|
import { CurrentUser } from '../auth/current-user.decorator';
|
2026-08-10 15:29:04 +03:30
|
|
|
import {
|
2026-10-07 20:48:32 +03:30
|
|
|
ANY_TENANT_READ,
|
|
|
|
|
productAnyReadPermissions,
|
2026-08-10 15:29:04 +03:30
|
|
|
productReadPermission,
|
2026-10-07 20:48:32 +03:30
|
|
|
productUiWritePermission,
|
2026-08-10 15:29:04 +03:30
|
|
|
productWritePermission,
|
2026-10-07 20:48:32 +03:30
|
|
|
userHasAnyPermission,
|
2026-08-10 15:29:04 +03:30
|
|
|
userHasPermission,
|
|
|
|
|
} from '../auth/permissions';
|
2026-07-30 18:19:53 +03:30
|
|
|
import {
|
|
|
|
|
AVATARS_DIR,
|
|
|
|
|
ensureUploadsDirs,
|
|
|
|
|
publicApiBase,
|
|
|
|
|
} from '../uploads/uploads.util';
|
2026-07-23 10:25:32 +03:30
|
|
|
import { CreateTenantDto } from './dto/create-tenant.dto';
|
|
|
|
|
import { UpdateTenantDto } from './dto/update-tenant.dto';
|
|
|
|
|
import { TenantsService } from './tenants.service';
|
|
|
|
|
|
2026-07-30 18:19:53 +03:30
|
|
|
const IMAGE_MIME = /^image\/(jpeg|png|gif|webp|svg\+xml)$/i;
|
|
|
|
|
|
2026-10-07 20:48:32 +03:30
|
|
|
/** Fields edited from the appearance pages; guarded by `<product>.ui:write`. */
|
|
|
|
|
const APPEARANCE_FIELDS = [
|
|
|
|
|
'appUiConfig',
|
|
|
|
|
'uiVariant',
|
|
|
|
|
'primaryColor',
|
|
|
|
|
'secondaryColor',
|
|
|
|
|
'backgroundColor',
|
|
|
|
|
] as const;
|
|
|
|
|
|
2026-08-10 15:29:04 +03:30
|
|
|
type AuthUser = { id: string; role?: string; permissions?: unknown };
|
|
|
|
|
|
2026-07-23 10:25:32 +03:30
|
|
|
@Controller('tenants')
|
2026-08-10 15:29:04 +03:30
|
|
|
@UseGuards(JwtAuthGuard, PermissionsGuard)
|
2026-07-23 10:25:32 +03:30
|
|
|
export class TenantsController {
|
2026-07-30 18:19:53 +03:30
|
|
|
constructor(
|
|
|
|
|
private readonly tenantsService: TenantsService,
|
|
|
|
|
private readonly config: ConfigService,
|
|
|
|
|
) {}
|
2026-07-23 10:25:32 +03:30
|
|
|
|
|
|
|
|
@Get()
|
2026-10-07 20:48:32 +03:30
|
|
|
@RequirePermissions(...ANY_TENANT_READ, { mode: 'any' })
|
2026-08-10 15:29:04 +03:30
|
|
|
async findAll(
|
|
|
|
|
@Query('product') product?: ProductType,
|
|
|
|
|
@CurrentUser() user?: AuthUser,
|
|
|
|
|
) {
|
|
|
|
|
if (product) {
|
2026-10-07 20:48:32 +03:30
|
|
|
this.assertAnyProductRead(user, product);
|
2026-08-10 15:29:04 +03:30
|
|
|
return this.tenantsService.findAll(product);
|
|
|
|
|
}
|
|
|
|
|
const rows = await this.tenantsService.findAll();
|
|
|
|
|
return rows.filter((row) =>
|
2026-10-07 20:48:32 +03:30
|
|
|
userHasAnyPermission(user || {}, productAnyReadPermissions(row.product)),
|
2026-08-10 15:29:04 +03:30
|
|
|
);
|
2026-07-23 10:25:32 +03:30
|
|
|
}
|
|
|
|
|
|
2026-07-30 18:19:53 +03:30
|
|
|
@Post('upload-avatar')
|
2026-10-07 16:52:23 +03:30
|
|
|
@RequirePermissions('hamkar:write', 'khanegi:write', 'others:write', { mode: 'any' })
|
2026-07-30 18:19:53 +03:30
|
|
|
@UseInterceptors(
|
|
|
|
|
FileInterceptor('file', {
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
destination: (_req, _file, cb) => {
|
|
|
|
|
ensureUploadsDirs();
|
|
|
|
|
cb(null, AVATARS_DIR);
|
|
|
|
|
},
|
|
|
|
|
filename: (_req, file, cb) => {
|
|
|
|
|
const safeExt = extname(file.originalname).toLowerCase() || '.png';
|
|
|
|
|
const name = `avatar_${Date.now()}_${Math.random().toString(36).slice(2, 8)}${safeExt}`;
|
|
|
|
|
cb(null, name);
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
limits: { fileSize: 2 * 1024 * 1024 },
|
|
|
|
|
fileFilter: (_req, file, cb) => {
|
|
|
|
|
if (!IMAGE_MIME.test(file.mimetype)) {
|
2026-08-10 15:29:04 +03:30
|
|
|
cb(
|
|
|
|
|
new BadRequestException(
|
|
|
|
|
'فقط تصویر (jpg/png/gif/webp/svg) مجاز است',
|
|
|
|
|
) as any,
|
|
|
|
|
false,
|
|
|
|
|
);
|
2026-07-30 18:19:53 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cb(null, true);
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
uploadAvatar(@UploadedFile() file: Express.Multer.File) {
|
|
|
|
|
if (!file) {
|
|
|
|
|
throw new BadRequestException('فایل ارسال نشده است');
|
|
|
|
|
}
|
|
|
|
|
const base = publicApiBase(this.config);
|
|
|
|
|
return { url: `${base}/uploads/avatars/${file.filename}` };
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 10:25:32 +03:30
|
|
|
@Get(':id')
|
2026-10-07 20:48:32 +03:30
|
|
|
@RequirePermissions(...ANY_TENANT_READ, { mode: 'any' })
|
2026-08-10 15:29:04 +03:30
|
|
|
async findOne(@Param('id') id: string, @CurrentUser() user: AuthUser) {
|
|
|
|
|
const tenant = await this.tenantsService.findOne(id);
|
2026-10-07 20:48:32 +03:30
|
|
|
this.assertAnyProductRead(user, tenant.product);
|
2026-08-10 15:29:04 +03:30
|
|
|
return tenant;
|
2026-07-23 10:25:32 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
2026-10-07 16:52:23 +03:30
|
|
|
@RequirePermissions('hamkar:write', 'khanegi:write', 'others:write', { mode: 'any' })
|
2026-08-10 15:29:04 +03:30
|
|
|
create(@Body() dto: CreateTenantDto, @CurrentUser() user: AuthUser) {
|
|
|
|
|
this.assertProductAccess(user, dto.product, 'write');
|
2026-07-23 10:25:32 +03:30
|
|
|
return this.tenantsService.create(dto, user.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch(':id')
|
2026-10-07 20:48:32 +03:30
|
|
|
@RequirePermissions(
|
|
|
|
|
'hamkar:write',
|
|
|
|
|
'hamkar.ui:write',
|
|
|
|
|
'khanegi:write',
|
|
|
|
|
'khanegi.ui:write',
|
|
|
|
|
'others:write',
|
|
|
|
|
{ mode: 'any' },
|
|
|
|
|
)
|
2026-08-10 15:29:04 +03:30
|
|
|
async update(
|
2026-07-23 10:25:32 +03:30
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Body() dto: UpdateTenantDto,
|
2026-08-10 15:29:04 +03:30
|
|
|
@CurrentUser() user: AuthUser,
|
2026-07-23 10:25:32 +03:30
|
|
|
) {
|
2026-08-10 15:29:04 +03:30
|
|
|
const tenant = await this.tenantsService.findOne(id);
|
2026-10-07 20:48:32 +03:30
|
|
|
const before = tenant as unknown as Record<string, unknown>;
|
|
|
|
|
const body = dto as Record<string, unknown>;
|
|
|
|
|
|
|
|
|
|
const appearanceChanged = APPEARANCE_FIELDS.some((key) => {
|
|
|
|
|
if (body[key] === undefined) return false;
|
|
|
|
|
return JSON.stringify(body[key]) !== JSON.stringify(before[key] ?? null);
|
|
|
|
|
});
|
|
|
|
|
const touchesIdentity = Object.keys(body).some(
|
|
|
|
|
(key) =>
|
|
|
|
|
body[key] !== undefined &&
|
|
|
|
|
!(APPEARANCE_FIELDS as readonly string[]).includes(key),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (touchesIdentity) {
|
|
|
|
|
this.assertProductAccess(user, tenant.product, 'write');
|
|
|
|
|
}
|
|
|
|
|
if (appearanceChanged) {
|
|
|
|
|
const uiCode = productUiWritePermission(tenant.product);
|
|
|
|
|
const allowed = uiCode
|
|
|
|
|
? userHasPermission(user || {}, uiCode)
|
|
|
|
|
: userHasPermission(user || {}, productWritePermission(tenant.product));
|
|
|
|
|
if (!allowed) {
|
|
|
|
|
throw new ForbiddenException('شما به ظاهر این محصول دسترسی ندارید');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!touchesIdentity && !appearanceChanged) {
|
|
|
|
|
this.assertAnyProductRead(user, tenant.product);
|
|
|
|
|
}
|
2026-07-23 10:25:32 +03:30
|
|
|
return this.tenantsService.update(id, dto, user.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete(':id')
|
2026-10-07 16:52:23 +03:30
|
|
|
@RequirePermissions('hamkar:write', 'khanegi:write', 'others:write', { mode: 'any' })
|
2026-08-10 15:29:04 +03:30
|
|
|
async remove(@Param('id') id: string, @CurrentUser() user: AuthUser) {
|
|
|
|
|
const tenant = await this.tenantsService.findOne(id);
|
|
|
|
|
this.assertProductAccess(user, tenant.product, 'write');
|
2026-07-23 10:25:32 +03:30
|
|
|
return this.tenantsService.remove(id, user.id);
|
|
|
|
|
}
|
2026-08-10 15:29:04 +03:30
|
|
|
|
|
|
|
|
private assertProductAccess(
|
|
|
|
|
user: AuthUser | undefined,
|
|
|
|
|
product: string,
|
|
|
|
|
mode: 'read' | 'write',
|
|
|
|
|
) {
|
|
|
|
|
const code =
|
|
|
|
|
mode === 'read'
|
|
|
|
|
? productReadPermission(product)
|
|
|
|
|
: productWritePermission(product);
|
|
|
|
|
if (!userHasPermission(user || {}, code)) {
|
|
|
|
|
throw new ForbiddenException('شما به این محصول دسترسی ندارید');
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-07 20:48:32 +03:30
|
|
|
|
|
|
|
|
private assertAnyProductRead(user: AuthUser | undefined, product: string) {
|
|
|
|
|
if (!userHasAnyPermission(user || {}, productAnyReadPermissions(product))) {
|
|
|
|
|
throw new ForbiddenException('شما به این محصول دسترسی ندارید');
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-23 10:25:32 +03:30
|
|
|
}
|