feat(prescriptions & auth): auto-create user and pet profile on prescription submit, validate mobile numbers with regex, allow public upload and auto-reset form state
Deploy Canina / deploy (push) Waiting to run

This commit is contained in:
parsa aghaei committed 2026-08-09 09:59:55 +03:30
1 parent b617331683
commit 4e304944ef
5 files changed
+103 -17

No files matched your search

+1 -2
View File
@@ -30,9 +30,8 @@ export class MediaController {
return { success: true, data };
}
@UseGuards(JwtAuthGuard)
@Post('upload')
@ApiOperation({ summary: 'آپلود فایل جدید' })
@ApiOperation({ summary: 'آپلود فایل جدید (عمومی/ادمین)' })
@UseInterceptors(FileInterceptor('file'))
async uploadFile(@UploadedFile() file: Express.Multer.File) {
if (!file) throw new BadRequestException('File is missing');
@@ -31,7 +31,14 @@ export class PrescriptionsController {
@ApiOperation({ summary: 'بارگذاری نسخه جدید توسط کاربر' })
create(
@Req() req: UserReqPayload,
@Body() body: { petId?: string; fileUrl: string; notes?: string },
@Body()
body: {
petId?: string;
petName?: string;
phone?: string;
fileUrl: string;
notes?: string;
},
) {
const userId = req.user?.id || req.user?.userId || null;
return this.prescriptionsService.create(userId, body);
@@ -11,17 +11,73 @@ export class PrescriptionsService {
async create(
userId: string | null,
data: { petId?: string; fileUrl: string; notes?: string },
data: {
petId?: string;
petName?: string;
phone?: string;
fileUrl: string;
notes?: string;
},
) {
let finalUserId = userId;
let finalPetId = data.petId || null;
// 1. If phone is provided, find or create user automatically
if (data.phone) {
const cleanPhone = data.phone.trim();
let user = await this.prisma.user.findFirst({
where: { mobile: cleanPhone },
});
if (!user) {
// Create user with default role Customer
user = await this.prisma.user.create({
data: {
mobile: cleanPhone,
role: 'Customer',
firstName: data.petName ? `سرپرست ${data.petName}` : 'کاربر',
lastName: 'کانینا',
},
});
}
finalUserId = user.id;
// 2. If petName is provided and user exists, find or create pet
if (data.petName && !finalPetId) {
const cleanPetName = data.petName.trim();
let pet = await this.prisma.pet.findFirst({
where: {
userId: user.id,
name: cleanPetName,
},
});
if (!pet) {
pet = await this.prisma.pet.create({
data: {
userId: user.id,
name: cleanPetName,
type: 'سگ', // Default pet type for prescription consultation
breed: 'نامشخص',
age: 1,
weight: 5.0,
activityLevel: 'متوسط',
},
});
}
finalPetId = pet.id;
}
}
return this.prisma.prescription.create({
data: {
userId: userId || undefined,
petId: data.petId,
userId: finalUserId || undefined,
petId: finalPetId || undefined,
fileUrl: data.fileUrl,
notes: data.notes,
status: 'PENDING',
},
include: { pet: true },
include: { user: true, pet: true },
});
}