forked from mtusi2026/mtusi2026lab3
fix: accept empty attachment_ids, normalize member uuid case, reject repeated query params
This commit is contained in:
@@ -20,19 +20,26 @@ export class ChatsService {
|
||||
|
||||
async create(
|
||||
title: string,
|
||||
memberIds: string[],
|
||||
rawMemberIds: string[],
|
||||
callerId: string,
|
||||
): Promise<Chat> {
|
||||
const invalid = rawMemberIds.find((id) => !isUuid(id));
|
||||
if (invalid) {
|
||||
throw new ApiError('invalid_request', `unknown member_id: ${invalid}`);
|
||||
}
|
||||
const memberIds = rawMemberIds.map((id) => id.toLowerCase());
|
||||
if (new Set(memberIds).size !== memberIds.length) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
"All member_ids's elements must be unique",
|
||||
);
|
||||
}
|
||||
if (!memberIds.includes(callerId)) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'member_ids must include the caller',
|
||||
);
|
||||
}
|
||||
const invalid = memberIds.find((id) => !isUuid(id));
|
||||
if (invalid) {
|
||||
throw new ApiError('invalid_request', `unknown member_id: ${invalid}`);
|
||||
}
|
||||
try {
|
||||
const chat = await this.prisma.$transaction(async (tx) => {
|
||||
const created = await tx.chat.create({
|
||||
|
||||
@@ -8,11 +8,10 @@ export function parseLimit(raw: unknown): number {
|
||||
if (raw === undefined) {
|
||||
return DEFAULT_LIMIT;
|
||||
}
|
||||
const value = Array.isArray(raw) ? raw[0] : raw;
|
||||
if (typeof value !== 'string' || !/^\d+$/.test(value)) {
|
||||
if (typeof raw !== 'string' || !/^\d+$/.test(raw)) {
|
||||
throw new ApiError('invalid_request', 'limit must be an integer');
|
||||
}
|
||||
const limit = Number(value);
|
||||
const limit = Number(raw);
|
||||
if (limit < MIN_LIMIT || limit > MAX_LIMIT) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
@@ -26,12 +25,11 @@ export function rawCursorParam(raw: unknown): string | null {
|
||||
if (raw === undefined) {
|
||||
return null;
|
||||
}
|
||||
const value = Array.isArray(raw) ? raw[0] : raw;
|
||||
if (typeof value !== 'string' || value.length === 0) {
|
||||
if (typeof raw !== 'string' || raw.length === 0) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'cursor must be a non-empty string',
|
||||
);
|
||||
}
|
||||
return value;
|
||||
return raw;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { IsString } from 'class-validator';
|
||||
import { ArrayMaxSize, IsArray, IsString, ValidateIf } from 'class-validator';
|
||||
import { CodePointLength } from '../../common/dto/code-point-length.validator';
|
||||
|
||||
export class CreateMessageDto {
|
||||
@IsString()
|
||||
@CodePointLength(0, 4000)
|
||||
text!: string;
|
||||
|
||||
@ValidateIf((dto: CreateMessageDto) => dto.attachment_ids !== undefined)
|
||||
@IsArray()
|
||||
@ArrayMaxSize(0, { message: 'attachment_ids must be empty: attachments are not supported' })
|
||||
attachment_ids?: unknown[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user