2026-08-29 21:06:30 +03:30
|
|
|
import {
|
2026-10-03 15:01:01 +03:30
|
|
|
BadRequestException,
|
2026-08-29 21:06:30 +03:30
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Param,
|
|
|
|
|
Patch,
|
|
|
|
|
Post,
|
|
|
|
|
Query,
|
2026-09-15 17:33:05 +03:30
|
|
|
Res,
|
|
|
|
|
StreamableFile,
|
|
|
|
|
UploadedFile,
|
2026-08-29 21:06:30 +03:30
|
|
|
UseGuards,
|
2026-09-15 17:33:05 +03:30
|
|
|
UseInterceptors,
|
2026-08-29 21:06:30 +03:30
|
|
|
} from '@nestjs/common';
|
2026-09-15 17:33:05 +03:30
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
|
|
|
import { memoryStorage } from 'multer';
|
|
|
|
|
import type { Response } from 'express';
|
2026-08-29 21:06:30 +03:30
|
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
2026-10-07 11:18:53 +03:30
|
|
|
import { RolesGuard } from '../auth/roles.guard';
|
|
|
|
|
import { Roles } from '../auth/roles.decorator';
|
2026-08-29 21:06:30 +03:30
|
|
|
import { CurrentUser } from '../auth/current-user.decorator';
|
2026-09-15 17:33:05 +03:30
|
|
|
import type { Actor } from '../common/record-scope';
|
2026-08-29 21:06:30 +03:30
|
|
|
import { ActivitiesService } from './activities.service';
|
2026-09-15 17:33:05 +03:30
|
|
|
import { ActivitiesExcelService } from './activities-excel.service';
|
2026-10-07 11:18:53 +03:30
|
|
|
import { ActivityStagesService } from './activity-stages.service';
|
2026-10-07 12:29:23 +03:30
|
|
|
import { ActivityTypesService } from './activity-types.service';
|
2026-08-29 21:06:30 +03:30
|
|
|
import {
|
2026-10-20 17:23:27 +03:30
|
|
|
CreateActivityBoardDto,
|
2026-08-29 21:06:30 +03:30
|
|
|
CreateActivityDto,
|
2026-10-07 11:18:53 +03:30
|
|
|
CreateActivityStageDto,
|
2026-10-07 12:29:23 +03:30
|
|
|
CreateActivityTypeDto,
|
2026-10-03 18:28:47 +03:30
|
|
|
ImportActivityRowsDto,
|
2026-08-29 21:06:30 +03:30
|
|
|
ListActivitiesQueryDto,
|
2026-10-20 17:23:27 +03:30
|
|
|
ListActivityStagesQueryDto,
|
2026-10-07 11:18:53 +03:30
|
|
|
MoveActivityStageDto,
|
|
|
|
|
ReorderActivityStagesDto,
|
2026-10-07 12:29:23 +03:30
|
|
|
ReorderActivityTypesDto,
|
2026-10-20 17:23:27 +03:30
|
|
|
UpdateActivityBoardDto,
|
2026-08-29 21:06:30 +03:30
|
|
|
UpdateActivityDto,
|
2026-10-07 11:18:53 +03:30
|
|
|
UpdateActivityStageDto,
|
2026-10-07 12:29:23 +03:30
|
|
|
UpdateActivityTypeDto,
|
2026-08-29 21:06:30 +03:30
|
|
|
} from './dto';
|
2026-09-15 17:33:05 +03:30
|
|
|
import { brand } from '@mento/shared';
|
2026-08-29 21:06:30 +03:30
|
|
|
|
2026-10-03 15:01:01 +03:30
|
|
|
const excelUpload = FileInterceptor('file', {
|
|
|
|
|
storage: memoryStorage(),
|
|
|
|
|
limits: { fileSize: 15_000_000 },
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
@Controller('activities')
|
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
export class ActivitiesController {
|
2026-09-15 17:33:05 +03:30
|
|
|
constructor(
|
|
|
|
|
private readonly activitiesService: ActivitiesService,
|
|
|
|
|
private readonly excelService: ActivitiesExcelService,
|
2026-10-07 11:18:53 +03:30
|
|
|
private readonly stagesService: ActivityStagesService,
|
2026-10-07 12:29:23 +03:30
|
|
|
private readonly typesService: ActivityTypesService,
|
2026-09-15 17:33:05 +03:30
|
|
|
) {}
|
2026-08-29 21:06:30 +03:30
|
|
|
|
|
|
|
|
@Get('summary')
|
2026-10-17 18:56:44 +03:30
|
|
|
summary(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Query('desk') desk?: string,
|
|
|
|
|
) {
|
|
|
|
|
const scope = desk === 'mine' || desk === 'team' ? desk : undefined;
|
|
|
|
|
return this.activitiesService.summary(user.organizationId, user, scope);
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
2026-10-20 17:23:27 +03:30
|
|
|
@Get('boards')
|
|
|
|
|
listBoards(@CurrentUser() user: Actor) {
|
|
|
|
|
return this.stagesService.listBoards(user.organizationId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('boards')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
createBoard(@CurrentUser() user: Actor, @Body() dto: CreateActivityBoardDto) {
|
|
|
|
|
return this.stagesService.createBoard(user.organizationId, dto.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch('boards/:id')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
updateBoard(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Body() dto: UpdateActivityBoardDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.stagesService.updateBoard(user.organizationId, id, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete('boards/:id')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
async removeBoard(@CurrentUser() user: Actor, @Param('id') id: string) {
|
|
|
|
|
await this.stagesService.removeBoard(user.organizationId, id);
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 11:18:53 +03:30
|
|
|
@Get('stages')
|
2026-10-20 17:23:27 +03:30
|
|
|
listStages(@CurrentUser() user: Actor, @Query() query: ListActivityStagesQueryDto) {
|
|
|
|
|
return this.stagesService.list(user.organizationId, query.boardId);
|
2026-10-07 11:18:53 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('stages')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
createStage(@CurrentUser() user: Actor, @Body() dto: CreateActivityStageDto) {
|
|
|
|
|
return this.stagesService.create(user.organizationId, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch('stages/reorder')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
reorderStages(@CurrentUser() user: Actor, @Body() dto: ReorderActivityStagesDto) {
|
|
|
|
|
return this.stagesService.reorder(user.organizationId, dto.stageIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch('stages/:id')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
updateStage(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Body() dto: UpdateActivityStageDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.stagesService.update(user.organizationId, id, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete('stages/:id')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
async removeStage(@CurrentUser() user: Actor, @Param('id') id: string) {
|
|
|
|
|
await this.stagesService.remove(user.organizationId, id);
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 12:29:23 +03:30
|
|
|
@Get('types')
|
|
|
|
|
listTypes(@CurrentUser() user: Actor) {
|
|
|
|
|
return this.typesService.list(user.organizationId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('types')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
createType(@CurrentUser() user: Actor, @Body() dto: CreateActivityTypeDto) {
|
|
|
|
|
return this.typesService.create(user.organizationId, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch('types/reorder')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
reorderTypes(@CurrentUser() user: Actor, @Body() dto: ReorderActivityTypesDto) {
|
|
|
|
|
return this.typesService.reorder(user.organizationId, dto.typeIds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch('types/:id')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
updateType(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Body() dto: UpdateActivityTypeDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.typesService.update(user.organizationId, id, dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete('types/:id')
|
|
|
|
|
@UseGuards(RolesGuard)
|
|
|
|
|
@Roles('owner', 'admin')
|
|
|
|
|
async removeType(@CurrentUser() user: Actor, @Param('id') id: string) {
|
|
|
|
|
await this.typesService.remove(user.organizationId, id);
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 11:18:53 +03:30
|
|
|
@Get('stage-counts')
|
|
|
|
|
stageCounts(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Query() query: ListActivitiesQueryDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.activitiesService.stageCounts(
|
|
|
|
|
user.organizationId,
|
|
|
|
|
user,
|
|
|
|
|
query.filter ?? 'all',
|
|
|
|
|
query.q,
|
|
|
|
|
query.desk,
|
|
|
|
|
{
|
|
|
|
|
type: query.type,
|
2026-10-07 12:29:23 +03:30
|
|
|
typeDefId: query.typeDefId,
|
2026-10-07 11:18:53 +03:30
|
|
|
status: query.status,
|
|
|
|
|
assigneeId: query.assigneeId,
|
2026-10-20 17:23:27 +03:30
|
|
|
boardId: query.boardId,
|
2026-10-07 11:18:53 +03:30
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
@Get()
|
|
|
|
|
list(
|
2026-09-15 17:33:05 +03:30
|
|
|
@CurrentUser() user: Actor,
|
2026-08-29 21:06:30 +03:30
|
|
|
@Query() query: ListActivitiesQueryDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.activitiesService.list(
|
|
|
|
|
user.organizationId,
|
|
|
|
|
user,
|
|
|
|
|
query.filter ?? 'today',
|
|
|
|
|
query.q,
|
|
|
|
|
query.page ?? 1,
|
|
|
|
|
query.limit ?? 50,
|
|
|
|
|
{
|
|
|
|
|
personId: query.personId,
|
|
|
|
|
companyId: query.companyId,
|
|
|
|
|
dealId: query.dealId,
|
2026-10-11 14:12:36 +03:30
|
|
|
leadId: query.leadId,
|
2026-08-29 21:06:30 +03:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: query.from,
|
|
|
|
|
to: query.to,
|
|
|
|
|
},
|
2026-09-28 12:28:14 +03:30
|
|
|
query.desk,
|
2026-10-03 12:00:46 +03:30
|
|
|
{
|
|
|
|
|
type: query.type,
|
2026-10-07 12:29:23 +03:30
|
|
|
typeDefId: query.typeDefId,
|
2026-10-03 12:00:46 +03:30
|
|
|
status: query.status,
|
|
|
|
|
assigneeId: query.assigneeId,
|
2026-10-20 17:23:27 +03:30
|
|
|
boardId: query.boardId,
|
2026-10-03 12:00:46 +03:30
|
|
|
},
|
2026-10-07 11:18:53 +03:30
|
|
|
query.stageId ? { stageId: query.stageId } : undefined,
|
2026-08-29 21:06:30 +03:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-15 17:33:05 +03:30
|
|
|
@Get('export')
|
|
|
|
|
async export(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Res({ passthrough: true }) res: Response,
|
|
|
|
|
) {
|
|
|
|
|
const buffer = await this.excelService.export(user);
|
|
|
|
|
res.set({
|
|
|
|
|
'Content-Type':
|
|
|
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
|
|
|
'Content-Disposition': `attachment; filename="${brand.filePrefix}-activities.xlsx"`,
|
|
|
|
|
});
|
|
|
|
|
return new StreamableFile(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-19 15:30:31 +03:30
|
|
|
@Post('import/preview')
|
2026-10-03 15:01:01 +03:30
|
|
|
@UseInterceptors(excelUpload)
|
|
|
|
|
importPreview(@UploadedFile() file?: Express.Multer.File) {
|
|
|
|
|
if (!file?.buffer?.length) {
|
|
|
|
|
throw new BadRequestException('فایل ارسال نشده');
|
|
|
|
|
}
|
2026-09-19 15:30:31 +03:30
|
|
|
return this.excelService.preview(file);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 17:58:41 +03:30
|
|
|
@Post('import/prepare')
|
|
|
|
|
@UseInterceptors(excelUpload)
|
|
|
|
|
importPrepare(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@UploadedFile() file?: Express.Multer.File,
|
|
|
|
|
@Body('mapping') mapping?: string,
|
|
|
|
|
) {
|
|
|
|
|
if (!file?.buffer?.length) {
|
|
|
|
|
throw new BadRequestException('فایل ارسال نشده');
|
|
|
|
|
}
|
|
|
|
|
return this.excelService.prepare(user, file, mapping);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 18:14:17 +03:30
|
|
|
@Get('import/prepare/:sessionId')
|
|
|
|
|
importPrepareStatus(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Param('sessionId') sessionId: string,
|
|
|
|
|
) {
|
|
|
|
|
return this.excelService.prepareStatus(user, sessionId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 18:28:47 +03:30
|
|
|
@Post('import/rows')
|
|
|
|
|
importRows(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Body() dto: ImportActivityRowsDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.excelService.importRows(user, dto);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-15 17:33:05 +03:30
|
|
|
@Post('import')
|
2026-10-03 15:01:01 +03:30
|
|
|
@UseInterceptors(excelUpload)
|
2026-09-15 17:33:05 +03:30
|
|
|
import(
|
|
|
|
|
@CurrentUser() user: Actor,
|
2026-10-03 15:01:01 +03:30
|
|
|
@UploadedFile() file?: Express.Multer.File,
|
2026-09-19 15:30:31 +03:30
|
|
|
@Body('mapping') mapping?: string,
|
2026-10-03 15:01:01 +03:30
|
|
|
@Body('sessionId') sessionId?: string,
|
|
|
|
|
@Body('offset') offset?: string,
|
|
|
|
|
@Body('limit') limit?: string,
|
2026-09-15 17:33:05 +03:30
|
|
|
) {
|
2026-10-03 15:01:01 +03:30
|
|
|
if (!file?.buffer?.length && !sessionId) {
|
|
|
|
|
throw new BadRequestException('فایل ارسال نشده');
|
|
|
|
|
}
|
|
|
|
|
return this.excelService.import(user, file, mapping, {
|
|
|
|
|
sessionId,
|
|
|
|
|
offset,
|
|
|
|
|
limit,
|
|
|
|
|
});
|
2026-09-15 17:33:05 +03:30
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
@Get(':id')
|
2026-09-15 17:33:05 +03:30
|
|
|
get(@CurrentUser() user: Actor, @Param('id') id: string) {
|
2026-08-29 21:06:30 +03:30
|
|
|
return this.activitiesService.get(user.organizationId, id, user);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 17:13:01 +03:30
|
|
|
@Post(':id/promote-from-prior')
|
|
|
|
|
promoteFromPrior(@CurrentUser() user: Actor, @Param('id') id: string) {
|
|
|
|
|
return this.activitiesService.promoteFromPrior(user, id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
@Post()
|
2026-09-15 17:33:05 +03:30
|
|
|
create(@CurrentUser() user: Actor, @Body() dto: CreateActivityDto) {
|
2026-08-29 21:06:30 +03:30
|
|
|
return this.activitiesService.create(
|
|
|
|
|
user.organizationId,
|
|
|
|
|
user.userId,
|
|
|
|
|
dto,
|
|
|
|
|
user,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch(':id')
|
|
|
|
|
update(
|
2026-09-15 17:33:05 +03:30
|
|
|
@CurrentUser() user: Actor,
|
2026-08-29 21:06:30 +03:30
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Body() dto: UpdateActivityDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.activitiesService.update(user.organizationId, id, user, dto);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 11:18:53 +03:30
|
|
|
@Patch(':id/stage')
|
|
|
|
|
moveStage(
|
|
|
|
|
@CurrentUser() user: Actor,
|
|
|
|
|
@Param('id') id: string,
|
|
|
|
|
@Body() dto: MoveActivityStageDto,
|
|
|
|
|
) {
|
|
|
|
|
return this.activitiesService.moveToStage(user.organizationId, id, user, dto.stageId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
@Patch(':id/complete')
|
2026-09-15 17:33:05 +03:30
|
|
|
complete(@CurrentUser() user: Actor, @Param('id') id: string) {
|
2026-08-29 21:06:30 +03:30
|
|
|
return this.activitiesService.complete(user.organizationId, id, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete(':id')
|
2026-09-15 17:33:05 +03:30
|
|
|
async remove(@CurrentUser() user: Actor, @Param('id') id: string) {
|
2026-08-29 21:06:30 +03:30
|
|
|
await this.activitiesService.remove(user.organizationId, id, user);
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}
|
|
|
|
|
}
|