98 lines
2.1 KiB
TypeScript
98 lines
2.1 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
Patch,
|
|
Post,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import {
|
|
IsArray,
|
|
IsIn,
|
|
IsOptional,
|
|
IsString,
|
|
MaxLength,
|
|
MinLength,
|
|
} from 'class-validator';
|
|
import { CUSTOMER_GROUP_COLORS, type CustomerGroupColor } from '@mento/shared';
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
import { RolesGuard } from '../auth/roles.guard';
|
|
import { Roles } from '../auth/roles.decorator';
|
|
import { CurrentUser } from '../auth/current-user.decorator';
|
|
import type { Actor } from '../common/record-scope';
|
|
import { CustomerGroupsService } from './customer-groups.service';
|
|
|
|
class CreateCustomerGroupDto {
|
|
@IsString()
|
|
@MinLength(1)
|
|
@MaxLength(80)
|
|
name!: string;
|
|
|
|
@IsOptional()
|
|
@IsIn([...CUSTOMER_GROUP_COLORS])
|
|
color?: CustomerGroupColor;
|
|
|
|
@IsOptional()
|
|
@IsArray()
|
|
@IsString({ each: true })
|
|
assigneeIds?: string[];
|
|
}
|
|
|
|
class UpdateCustomerGroupDto {
|
|
@IsOptional()
|
|
@IsString()
|
|
@MinLength(1)
|
|
@MaxLength(80)
|
|
name?: string;
|
|
|
|
@IsOptional()
|
|
@IsIn([...CUSTOMER_GROUP_COLORS])
|
|
color?: CustomerGroupColor;
|
|
|
|
/** null گروه را اصلی میکند */
|
|
@IsOptional()
|
|
@IsString()
|
|
parentId?: string | null;
|
|
|
|
@IsOptional()
|
|
@IsArray()
|
|
@IsString({ each: true })
|
|
assigneeIds?: string[];
|
|
}
|
|
|
|
@Controller('customer-groups')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
export class CustomerGroupsController {
|
|
constructor(private readonly groups: CustomerGroupsService) {}
|
|
|
|
@Get()
|
|
list(@CurrentUser() user: Actor) {
|
|
return this.groups.list(user.organizationId);
|
|
}
|
|
|
|
@Post()
|
|
@Roles('owner', 'admin')
|
|
create(@CurrentUser() user: Actor, @Body() dto: CreateCustomerGroupDto) {
|
|
return this.groups.create(user.organizationId, dto);
|
|
}
|
|
|
|
@Patch(':id')
|
|
@Roles('owner', 'admin')
|
|
update(
|
|
@CurrentUser() user: Actor,
|
|
@Param('id') id: string,
|
|
@Body() dto: UpdateCustomerGroupDto,
|
|
) {
|
|
return this.groups.update(user.organizationId, id, dto);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@Roles('owner', 'admin')
|
|
async remove(@CurrentUser() user: Actor, @Param('id') id: string) {
|
|
await this.groups.remove(user.organizationId, id);
|
|
return { ok: true };
|
|
}
|
|
}
|