2025-10-27 06:41:51 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
|
import { Prisma, rekam_medis } from '@dist/generated/prisma';
|
|
|
|
|
import { CreateRekamMedisDto } from './dto/create-rekammedis.dto';
|
|
|
|
|
import { CreateLogDto } from '../log/dto/create-log.dto';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class RekammedisService {
|
|
|
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
async getAllRekamMedis(params: {
|
|
|
|
|
take?: number;
|
|
|
|
|
skip?: number;
|
|
|
|
|
page?: number;
|
|
|
|
|
orderBy?: any;
|
|
|
|
|
no_rm?: string;
|
|
|
|
|
order?: 'asc' | 'desc';
|
2025-10-30 05:15:06 +00:00
|
|
|
}) {
|
2025-10-27 06:41:51 +00:00
|
|
|
const { skip, page, orderBy, order, no_rm } = params;
|
|
|
|
|
const take = params.take ? parseInt(params.take.toString()) : 10;
|
|
|
|
|
const skipValue = skip
|
|
|
|
|
? parseInt(skip.toString())
|
|
|
|
|
: page
|
|
|
|
|
? (parseInt(page.toString()) - 1) * take
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
|
|
const results = await this.prisma.rekam_medis.findMany({
|
|
|
|
|
skip: skipValue,
|
|
|
|
|
take: take,
|
|
|
|
|
where: {
|
|
|
|
|
no_rm: no_rm ? no_rm : undefined,
|
|
|
|
|
},
|
|
|
|
|
orderBy: orderBy
|
|
|
|
|
? { [orderBy]: order || 'asc' }
|
|
|
|
|
: { waktu_visit: order ? order : 'asc' },
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-30 05:15:06 +00:00
|
|
|
const count = await this.prisma.rekam_medis.count({
|
|
|
|
|
where: {
|
|
|
|
|
no_rm: no_rm ? { contains: no_rm } : undefined,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// console.log('Fetched Rekam Medis:', count);
|
|
|
|
|
return {
|
|
|
|
|
...results,
|
|
|
|
|
totalCount: count,
|
|
|
|
|
};
|
2025-10-27 06:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createRekamMedis(data: CreateRekamMedisDto) {
|
|
|
|
|
const latestId = await this.prisma.rekam_medis.findFirst({
|
|
|
|
|
orderBy: { waktu_visit: 'desc' },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let newId = '';
|
|
|
|
|
let xCounter = 0;
|
|
|
|
|
let rekamMedis: Prisma.rekam_medisCreateInput;
|
|
|
|
|
|
|
|
|
|
for (let i = (latestId?.id_visit?.length ?? 0) - 1; i >= 0; i--) {
|
|
|
|
|
if (latestId?.id_visit[i] === 'X') {
|
|
|
|
|
xCounter++;
|
|
|
|
|
} else {
|
|
|
|
|
newId = latestId?.id_visit?.substring(0, i + 1) || '';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (xCounter < 1) {
|
|
|
|
|
newId = (parseInt(latestId?.id_visit || '0', 10) + 1).toString();
|
|
|
|
|
} else {
|
|
|
|
|
newId = (parseInt(newId || '0', 10) + 1).toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rekamMedis = {
|
|
|
|
|
...data,
|
|
|
|
|
id_visit: newId,
|
|
|
|
|
waktu_visit: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const logData: CreateLogDto = {
|
|
|
|
|
event: 'rekam_medis_created',
|
|
|
|
|
payload: {
|
|
|
|
|
dokter_id: 123,
|
|
|
|
|
visit_id: newId,
|
|
|
|
|
anamnese: data.anamnese,
|
|
|
|
|
jenis_kasus: data.jenis_kasus,
|
|
|
|
|
tindak_lanjut: data.tindak_lanjut,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const newRekamMedis = await this.prisma.$transaction(async (tx) => {
|
|
|
|
|
const createdRekamMedis = await tx.rekam_medis.create({
|
|
|
|
|
data: rekamMedis,
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-30 05:15:06 +00:00
|
|
|
await tx.blockchain_log_queue.create({
|
|
|
|
|
data: {
|
|
|
|
|
event: logData.event,
|
|
|
|
|
user_id: 9,
|
|
|
|
|
payload: logData.payload,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Input Into Fabric Here
|
2025-10-27 06:41:51 +00:00
|
|
|
|
|
|
|
|
return createdRekamMedis;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return newRekamMedis;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error creating Rekam Medis:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|