formulario de capacitacion
This commit is contained in:
118
apps/api/src/features/training/training.service.ts
Normal file
118
apps/api/src/features/training/training.service.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { DRIZZLE_PROVIDER } from 'src/database/drizzle-provider';
|
||||
import { Inject, Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import * as schema from 'src/database/index';
|
||||
import { trainingSurveys } from 'src/database/index';
|
||||
import { eq, like, or, SQL, sql } from 'drizzle-orm';
|
||||
import { CreateTrainingDto } from './dto/create-training.dto';
|
||||
import { UpdateTrainingDto } from './dto/update-training.dto';
|
||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||
|
||||
@Injectable()
|
||||
export class TrainingService {
|
||||
constructor(
|
||||
@Inject(DRIZZLE_PROVIDER) private drizzle: NodePgDatabase<typeof schema>,
|
||||
) { }
|
||||
|
||||
async findAll(paginationDto?: PaginationDto) {
|
||||
const { page = 1, limit = 10, search = '', sortBy = 'id', sortOrder = 'asc' } = paginationDto || {};
|
||||
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
let searchCondition: SQL<unknown> | undefined;
|
||||
if (search) {
|
||||
searchCondition = or(
|
||||
like(trainingSurveys.firstname, `%${search}%`),
|
||||
like(trainingSurveys.lastname, `%${search}%`),
|
||||
like(trainingSurveys.ospName, `%${search}%`),
|
||||
like(trainingSurveys.ospRif, `%${search}%`)
|
||||
);
|
||||
}
|
||||
|
||||
const orderBy = sortOrder === 'asc'
|
||||
? sql`${trainingSurveys[sortBy as keyof typeof trainingSurveys]} asc`
|
||||
: sql`${trainingSurveys[sortBy as keyof typeof trainingSurveys]} desc`;
|
||||
|
||||
const totalCountResult = await this.drizzle
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(trainingSurveys)
|
||||
.where(searchCondition);
|
||||
|
||||
const totalCount = Number(totalCountResult[0].count);
|
||||
const totalPages = Math.ceil(totalCount / limit);
|
||||
|
||||
const data = await this.drizzle
|
||||
.select()
|
||||
.from(trainingSurveys)
|
||||
.where(searchCondition)
|
||||
.orderBy(orderBy)
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
|
||||
const meta = {
|
||||
page,
|
||||
limit,
|
||||
totalCount,
|
||||
totalPages,
|
||||
hasNextPage: page < totalPages,
|
||||
hasPreviousPage: page > 1,
|
||||
nextPage: page < totalPages ? page + 1 : null,
|
||||
previousPage: page > 1 ? page - 1 : null,
|
||||
};
|
||||
|
||||
return { data, meta };
|
||||
}
|
||||
|
||||
async findOne(id: number) {
|
||||
const find = await this.drizzle
|
||||
.select()
|
||||
.from(trainingSurveys)
|
||||
.where(eq(trainingSurveys.id, id));
|
||||
|
||||
if (find.length === 0) {
|
||||
throw new HttpException('Training record not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return find[0];
|
||||
}
|
||||
|
||||
async create(createTrainingDto: CreateTrainingDto) {
|
||||
const [newRecord] = await this.drizzle
|
||||
.insert(trainingSurveys)
|
||||
.values({
|
||||
...createTrainingDto,
|
||||
visitDate: new Date(createTrainingDto.visitDate),
|
||||
})
|
||||
.returning();
|
||||
|
||||
return newRecord;
|
||||
}
|
||||
|
||||
async update(id: number, updateTrainingDto: UpdateTrainingDto) {
|
||||
await this.findOne(id);
|
||||
|
||||
const updateData: any = { ...updateTrainingDto };
|
||||
if (updateTrainingDto.visitDate) {
|
||||
updateData.visitDate = new Date(updateTrainingDto.visitDate);
|
||||
}
|
||||
|
||||
const [updatedRecord] = await this.drizzle
|
||||
.update(trainingSurveys)
|
||||
.set(updateData)
|
||||
.where(eq(trainingSurveys.id, id))
|
||||
.returning();
|
||||
|
||||
return updatedRecord;
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
await this.findOne(id);
|
||||
|
||||
const [deletedRecord] = await this.drizzle
|
||||
.delete(trainingSurveys)
|
||||
.where(eq(trainingSurveys.id, id))
|
||||
.returning();
|
||||
|
||||
return { message: 'Training record deleted successfully', data: deletedRecord };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user