forked from Yara724/api
Fix SMS, and added pagination+sorting+searching for GETs
This commit is contained in:
23
src/request-management/dto/blame-list-user-v2.dto.ts
Normal file
23
src/request-management/dto/blame-list-user-v2.dto.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
|
||||
/** User / field-expert blame list (v2 GET /v2/blame-request-management). */
|
||||
export class GetUserBlameListV2ResponseDto {
|
||||
@ApiProperty({
|
||||
type: "array",
|
||||
description: "Blame requests for the current user (parties stripped)",
|
||||
items: { type: "object", additionalProperties: true },
|
||||
})
|
||||
list: Record<string, unknown>[];
|
||||
|
||||
@ApiProperty({ description: "Count after `search` filter" })
|
||||
total: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
page?: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
limit?: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
totalPages?: number;
|
||||
}
|
||||
@@ -42,6 +42,9 @@ import { StepsEnum } from "src/Types&Enums/blame-request-management/steps.enum";
|
||||
import { ExpertDbService } from "src/users/entities/db-service/expert.db.service";
|
||||
import { UserDbService } from "src/users/entities/db-service/user.db.service";
|
||||
import { parseIranLocalDateTime } from "src/helpers/iran-datetime";
|
||||
import { applyListQueryV2 } from "src/helpers/list-query-v2";
|
||||
import { ListQueryV2Dto } from "src/common/dto/list-query-v2.dto";
|
||||
import { GetUserBlameListV2ResponseDto } from "src/request-management/dto/blame-list-user-v2.dto";
|
||||
import { AutoCloseRequestService } from "src/utils/cron/cron.service";
|
||||
import { SmsOrchestrationService } from "src/sms-orchestration/sms-orchestration.service";
|
||||
import {
|
||||
@@ -4147,7 +4150,10 @@ export class RequestManagementService {
|
||||
}));
|
||||
}
|
||||
|
||||
async getAllBlameRequestsV2(user: any): Promise<any> {
|
||||
async getAllBlameRequestsV2(
|
||||
user: any,
|
||||
query: ListQueryV2Dto = {},
|
||||
): Promise<GetUserBlameListV2ResponseDto> {
|
||||
try {
|
||||
const userIdFilter =
|
||||
user?.sub && Types.ObjectId.isValid(user.sub)
|
||||
@@ -4158,13 +4164,16 @@ export class RequestManagementService {
|
||||
: null;
|
||||
|
||||
const filters = [userIdFilter, phoneFilter].filter(Boolean);
|
||||
if (filters.length === 0) return [];
|
||||
if (filters.length === 0) {
|
||||
return { list: [], total: 0 };
|
||||
}
|
||||
|
||||
const requests = await this.blameRequestDbService.find(
|
||||
filters.length === 1 ? (filters[0] as any) : ({ $or: filters } as any),
|
||||
{
|
||||
select: "requestNo type status blameStatus createdAt updatedAt parties",
|
||||
}
|
||||
select:
|
||||
"publicId requestNo type status blameStatus createdAt updatedAt parties",
|
||||
},
|
||||
);
|
||||
|
||||
const enriched = requests.map((req: any) => {
|
||||
@@ -4184,7 +4193,33 @@ export class RequestManagementService {
|
||||
};
|
||||
});
|
||||
|
||||
return enriched;
|
||||
const paged = applyListQueryV2(enriched, {
|
||||
publicId: (r) => String((r as { publicId?: string }).publicId ?? ""),
|
||||
createdAt: (r) => (r as { createdAt?: Date }).createdAt,
|
||||
requestNo: (r) => String((r as { requestNo?: string }).requestNo ?? ""),
|
||||
status: (r) => String((r as { status?: string }).status ?? ""),
|
||||
searchExtras: (r) => {
|
||||
const row = r as {
|
||||
blameStatus?: string;
|
||||
type?: string;
|
||||
userSide?: string;
|
||||
};
|
||||
return [
|
||||
row.blameStatus,
|
||||
row.type,
|
||||
row.userSide,
|
||||
String((r as { _id?: unknown })._id ?? ""),
|
||||
].filter(Boolean) as string[];
|
||||
},
|
||||
}, query);
|
||||
|
||||
return {
|
||||
list: paged.list,
|
||||
total: paged.total,
|
||||
page: paged.page,
|
||||
limit: paged.limit,
|
||||
totalPages: paged.totalPages,
|
||||
};
|
||||
} catch (err) {
|
||||
this.logger.error("Error: ", err);
|
||||
throw new InternalServerErrorException("Something Went Wrong");
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Req,
|
||||
UploadedFile,
|
||||
UploadedFiles,
|
||||
@@ -39,6 +40,8 @@ import {
|
||||
CarBodyFormDto,
|
||||
} from "./dto/create-request-management.dto";
|
||||
import { RequestManagementService } from "./request-management.service";
|
||||
import { ListQueryV2Dto } from "src/common/dto/list-query-v2.dto";
|
||||
import { GetUserBlameListV2ResponseDto } from "./dto/blame-list-user-v2.dto";
|
||||
|
||||
/**
|
||||
* V2 API surface for the redesigned `BlameRequest` model.
|
||||
@@ -71,10 +74,16 @@ export class RequestManagementV2Controller {
|
||||
|
||||
@Get()
|
||||
@Roles(RoleEnum.USER, RoleEnum.FIELD_EXPERT)
|
||||
@ApiOperation({
|
||||
summary: "List my blame requests (V2)",
|
||||
description:
|
||||
"All blame files for the current user. Optional query: `search`, `sortBy` (publicId | createdAt | requestNo | status), `sortOrder`, `page`, `limit`. Without `page`/`limit`, returns the full filtered list.",
|
||||
})
|
||||
async getAllBlameRequestsV2(
|
||||
@CurrentUser() user: any,
|
||||
) {
|
||||
return this.requestManagementService.getAllBlameRequestsV2(user);
|
||||
@Query() query: ListQueryV2Dto,
|
||||
): Promise<GetUserBlameListV2ResponseDto> {
|
||||
return this.requestManagementService.getAllBlameRequestsV2(user, query);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user