feat(backend): implement Phase A database models and controllers for cross-boundary dependencies
Deploy Canina / deploy (push) Failing after 28s
Deploy Canina / deploy (push) Failing after 28s
This commit is contained in:
1 parent
74fcaa6a4e
commit
1ae4c99ffc
26 files changed
+1205
-345
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Patch,
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { PrescriptionsService } from './prescriptions.service';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
|
||||
@ApiTags('Prescriptions - نسخه و تاییدیه دارویی')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Controller('prescriptions')
|
||||
export class PrescriptionsController {
|
||||
constructor(private readonly prescriptionsService: PrescriptionsService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'بارگذاری نسخه جدید توسط کاربر' })
|
||||
create(
|
||||
@Req() req: any,
|
||||
@Body() body: { petId?: string; fileUrl: string; notes?: string },
|
||||
) {
|
||||
const userId = req.user.id || req.user.userId;
|
||||
return this.prescriptionsService.create(userId, body);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'دریافت لیست نسخهها (کاربر یا ادمین)' })
|
||||
findAll(@Req() req: any) {
|
||||
const userId = req.user.id || req.user.userId;
|
||||
const isAdmin = req.user.role === 'Admin';
|
||||
return this.prescriptionsService.findAll(userId, isAdmin);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'دریافت جزئیات یک نسخه' })
|
||||
findOne(@Req() req: any, @Param('id') id: string) {
|
||||
const userId = req.user.id || req.user.userId;
|
||||
const isAdmin = req.user.role === 'Admin';
|
||||
return this.prescriptionsService.findOne(id, userId, isAdmin);
|
||||
}
|
||||
|
||||
@UseGuards(RolesGuard)
|
||||
@Roles('Admin')
|
||||
@Patch(':id/review')
|
||||
@ApiOperation({ summary: 'بررسی و تایید/رد نسخه دارویی (نیازمند ادمین)' })
|
||||
review(
|
||||
@Param('id') id: string,
|
||||
@Body()
|
||||
body: { status: 'APPROVED' | 'REJECTED' | 'PENDING'; adminNotes?: string },
|
||||
) {
|
||||
return this.prescriptionsService.review(id, body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PrescriptionsController } from './prescriptions.controller';
|
||||
import { PrescriptionsService } from './prescriptions.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [PrescriptionsController],
|
||||
providers: [PrescriptionsService],
|
||||
exports: [PrescriptionsService],
|
||||
})
|
||||
export class PrescriptionsModule {}
|
||||
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class PrescriptionsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async create(
|
||||
userId: string,
|
||||
data: { petId?: string; fileUrl: string; notes?: string },
|
||||
) {
|
||||
return this.prisma.prescription.create({
|
||||
data: {
|
||||
userId,
|
||||
petId: data.petId,
|
||||
fileUrl: data.fileUrl,
|
||||
notes: data.notes,
|
||||
status: 'PENDING',
|
||||
},
|
||||
include: { pet: true },
|
||||
});
|
||||
}
|
||||
|
||||
async findAll(userId: string, isAdmin: boolean) {
|
||||
if (isAdmin) {
|
||||
return this.prisma.prescription.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: { user: true, pet: true },
|
||||
});
|
||||
}
|
||||
return this.prisma.prescription.findMany({
|
||||
where: { userId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: { pet: true },
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string, userId: string, isAdmin: boolean) {
|
||||
const item = await this.prisma.prescription.findUnique({
|
||||
where: { id },
|
||||
include: { user: true, pet: true },
|
||||
});
|
||||
|
||||
if (!item) {
|
||||
throw new NotFoundException(`Prescription with ID ${id} not found`);
|
||||
}
|
||||
|
||||
if (!isAdmin && item.userId !== userId) {
|
||||
throw new ForbiddenException('Access denied to this prescription');
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
async review(
|
||||
id: string,
|
||||
reviewData: {
|
||||
status: 'APPROVED' | 'REJECTED' | 'PENDING';
|
||||
adminNotes?: string;
|
||||
},
|
||||
) {
|
||||
const item = await this.prisma.prescription.findUnique({ where: { id } });
|
||||
if (!item) {
|
||||
throw new NotFoundException(`Prescription with ID ${id} not found`);
|
||||
}
|
||||
return this.prisma.prescription.update({
|
||||
where: { id },
|
||||
data: {
|
||||
status: reviewData.status,
|
||||
adminNotes: reviewData.adminNotes,
|
||||
},
|
||||
include: { user: true, pet: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user