fix import patient

This commit is contained in:
nochill 2024-02-23 14:10:40 +07:00
parent 1db9d43b64
commit 3e6f91f0ab
9 changed files with 125 additions and 46 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
don't look at the patient repository
it's only psycho who does that,
should've just update everything from the payload

View File

@ -2,6 +2,7 @@ package repository
import (
"context"
"time"
"git.nochill.in/nochill/excel_import_playground/model"
"github.com/jackc/pgx/v5"
@ -22,11 +23,10 @@ func (q *Queries) CreateAlamat(ctx context.Context, arg model.Alamat) (model.Ala
rw,
no_hp,
no_telpon,
pasen_id,
pasien_id,
status,
created_at,
updated_at,
) VALUES ($1,, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
updated_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
RETURNING *
`
@ -45,6 +45,7 @@ func (q *Queries) CreateAlamat(ctx context.Context, arg model.Alamat) (model.Ala
arg.No_telpon,
arg.Pasien_id,
arg.Status,
time.Now().UTC(),
)
result, err := pgx.CollectExactlyOneRow[model.Alamat](row, pgx.RowToStructByName[model.Alamat])

View File

@ -21,7 +21,7 @@ func (q *Queries) CreateKeluarga(ctx context.Context, arg model.Keluarga) (model
jenis_kelamin,
pendidikan,
pekerjaan,
email,
email
) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
`

View File

@ -2,6 +2,7 @@ package repository
import (
"context"
"strconv"
"strings"
"time"
@ -20,7 +21,7 @@ func (q *Queries) FindLastPatientInCertainFasyankes(ctx context.Context, fasyank
LIMIT(1)
`
row, _ := q.db.Query(ctx, findLastPasienQuery, fasyankes_id)
result, err := pgx.CollectExactlyOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
result, err := pgx.CollectOneRow[model.Patient](row, pgx.RowToStructByName[model.Patient])
return result, err
}
@ -179,7 +180,36 @@ func (q *Queries) UpdatePatientNik(ctx context.Context, nik *string, patientId i
}
func (q *Queries) CreatePatient(ctx context.Context, arg model.Patient) (model.Patient, error) {
type createPatientParams struct {
NoRm string `json:"no_rm" db:"no_rm"`
NamaPasien string `json:"nama_pasien" db:"nama_pasien"`
JenisIdentitas pgtype.Int4 `json:"jenis_identitas" db:"jenis_identitas"`
NoIdentitas pgtype.Text `json:"no_identitas" db:"no_identitas"`
FotoProfil pgtype.Text `json:"foto_profil" db:"foto_profil"`
FotoKtp pgtype.Text `json:"foto_ktp" db:"foto_ktp"`
KotaLahir pgtype.Int4 `json:"kota_lahir" db:"kota_lahir"`
TanggalLahir time.Time `json:"tanggal_lahir" db:"tanggal_lahir"`
JenisKelamin int8 `json:"jenis_kelamin" db:"jenis_kelamin"`
Suku pgtype.Int4 `json:"suku" db:"suku"`
Agama pgtype.Int4 `json:"agama" db:"agama"`
Kebangsaan pgtype.Int4 `json:"kebangsaan" db:"kebangsaan"`
Bahasa pgtype.Int4 `json:"bahasa" db:"bahasa"`
Pendidikan pgtype.Int4 `json:"pendidikan" db:"pendidikan"`
StatusPerkawinan pgtype.Int4 `json:"status_perkawinan" db:"status_perkawinan"`
Email pgtype.Text `json:"email" db:"email"`
Pekerjaan pgtype.Int4 `json:"pekerjaan" db:"pekerjaan"`
NoHp pgtype.Text `json:"no_hp" db:"no_hp"`
IsDeleted bool `json:"is_deleted" db:"is_deleted"`
FasyankesID int `json:"fasyankes_id" db:"fasyankes_id"`
Nik pgtype.Text `json:"nik" db:"nik"`
NoKk pgtype.Text `json:"no_kk" db:"no_kk"`
NoKartuPesertaBPJS pgtype.Text `json:"no_kartu_peserta_bpjs" db:"no_kartu_peserta_bpjs"`
CreateBy int `json:"create_by" db:"create_by"`
UpdateBy int `json:"update_by" db:"update_by"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
func (q *Queries) CreatePatient(ctx context.Context, arg createPatientParams) (model.Patient, error) {
createPasienQuery := `
INSERT INTO "Pasien"(
no_rm,
@ -206,8 +236,9 @@ func (q *Queries) CreatePatient(ctx context.Context, arg model.Patient) (model.P
no_kk,
no_kartu_peserta_bpjs,
create_by,
update_by
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
update_by,
updated_at
) VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26)
RETURNING *;
`
@ -237,7 +268,6 @@ func (q *Queries) CreatePatient(ctx context.Context, arg model.Patient) (model.P
arg.NoKartuPesertaBPJS,
arg.CreateBy,
arg.UpdateBy,
arg.CreatedAt,
arg.UpdatedAt,
)
@ -276,7 +306,7 @@ type ImportDataPasienParams struct {
AlamatPenjamin pgtype.Text
}
func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPasienParams) error {
func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPasienParams, createdBy int32, fasyankesId int32) error {
err := store.execTx(ctx, func(q *Queries) error {
var err error
@ -298,7 +328,7 @@ func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPas
params.StatusNikah = &tempNilInt
}
patient, err := q.CreatePatient(ctx, model.Patient{
patient, err := q.CreatePatient(ctx, createPatientParams{
NoRm: params.NoRekamMedis,
NamaPasien: params.NamaPasien,
Pendidikan: pgtype.Int4{Valid: params.Pendidikan != nil, Int32: int32(*params.Pendidikan)},
@ -312,6 +342,19 @@ func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPas
JenisKelamin: params.Kelamin,
StatusPerkawinan: pgtype.Int4{Valid: params.StatusNikah != nil, Int32: int32(*params.StatusNikah)},
IsDeleted: false,
JenisIdentitas: pgtype.Int4{Valid: false, Int32: 0},
NoIdentitas: pgtype.Text{Valid: false, String: ""},
FotoProfil: pgtype.Text{Valid: false, String: ""},
FotoKtp: pgtype.Text{Valid: false, String: ""},
KotaLahir: pgtype.Int4{Valid: false, Int32: 0},
Suku: pgtype.Int4{Valid: false, Int32: 0},
Kebangsaan: pgtype.Int4{Valid: false, Int32: 0},
Bahasa: pgtype.Int4{Valid: false, Int32: 0},
FasyankesID: int(fasyankesId),
CreateBy: int(createdBy),
UpdateBy: int(createdBy),
NoKk: pgtype.Text{Valid: false, String: ""},
UpdatedAt: time.Now().UTC(),
})
if err != nil {
@ -323,8 +366,10 @@ func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPas
provinsi, err := q.FindWilayahByName(ctx, *params.Provinsi)
if err != nil {
if err != pgx.ErrNoRows {
return err
}
}
if provinsi.Id == 0 {
provinsi_id = nil
@ -336,6 +381,17 @@ func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPas
Provinsi: provinsi_id,
Nama_jalan: &params.NamaJalan.String,
Pasien_id: int32(patient.ID),
Kategori: nil,
Negara: nil,
Kabupaten: nil,
Kecamatan: nil,
Kelurahan: nil,
Kode_pos: nil,
Rt: nil,
Rw: nil,
No_hp: nil,
No_telpon: nil,
Status: nil,
})
if err != nil {
@ -378,14 +434,18 @@ func genNoRm(lastNoRm *string) string {
if lastNoRm != nil {
tempNoRm = *lastNoRm
} else {
tempNoRm = "1"
tempNoRm = "0"
}
length := strings.IndexFunc(tempNoRm, func(r rune) bool {
return r != '0'
})
return util.Pad(tempNoRm, int32(length))
}
if lastNoRm == nil {
length = 6
}
// func FindPatientBy
tempNoRmNum, _ := strconv.ParseInt(tempNoRm, 10, 32)
return util.PadWithZeros(int(tempNoRmNum)+1, int(length))
}

View File

@ -9,7 +9,7 @@ import (
type Store interface {
Querier
ImportPatientTx(ctx context.Context, params ImportDataPasienParams) error
ImportPatientTx(ctx context.Context, params ImportDataPasienParams, createdBy int32, fasyankesId int32) error
}
type SQLStore struct {

View File

@ -10,7 +10,7 @@ import (
func (q *Queries) FindWilayahByName(ctx context.Context, name string) (model.Wilayah, error) {
findWilayaByNameQuery := `
SELECT * FROM "Wilayah"
WHERE NamaWilayah = $1
WHERE nama_wilayah = $1
`
row, _ := q.db.Query(ctx, findWilayaByNameQuery, name)

View File

@ -14,6 +14,7 @@ import (
"git.nochill.in/nochill/excel_import_playground/internal/repository"
"git.nochill.in/nochill/excel_import_playground/util"
"github.com/gorilla/mux"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/xuri/excelize/v2"
)
@ -139,6 +140,7 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) {
patientExist, err := repository.Store.FindPatientByNoRm(s.Store, r.Context(), value.NoRekamMedis)
if err != nil {
if err != pgx.ErrNoRows {
errorMsg = append(errorMsg, map[string]any{
"no_rm": value.NoRekamMedis,
"status": "CRITICAL",
@ -146,6 +148,7 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) {
})
continue
}
}
if patientExist.NoRm != "" {
if value.NoRekamMedis == patientExist.NoRm {
@ -173,12 +176,14 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) {
patient, err := repository.Store.FindPatientByNik(s.Store, r.Context(), value.Nik.String)
if err != nil {
if err != pgx.ErrNoRows {
errorMsg = append(errorMsg, map[string]any{
"no_rm": value.NoRekamMedis,
"status": "CRITICAL",
"message": fmt.Sprintf("Terjadi kesalahan sistem, gagal mendapatakan data pasien %v", err),
})
}
}
if value.Nik.String == patient.Nik.String {
if patient.TanggalLahir.Before(value.TglLahir) {
@ -200,7 +205,7 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) {
errorMsg = append(errorMsg, map[string]any{
"no_rm": value.NoRekamMedis,
"status": "WARNING",
"message": fmt.Sprintf("NIK kembar dengan pasien: %s, mohon dicek kembali current user: %v", patient.NoRm, value),
"message": fmt.Sprintf("NIK kembar dengan pasien yang ada di sistem noRm: %s %s, mohon dicek kembali current user: %v"),
})
}
@ -212,12 +217,14 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) {
patient, err := repository.Store.FindPatientByBPJSCode(s.Store, r.Context(), value.NoBPJS.String)
if err != nil {
if err != pgx.ErrNoRows {
errorMsg = append(errorMsg, map[string]any{
"no_rm": value.NoRekamMedis,
"status": "CRITICAL",
"message": fmt.Sprintf("Terjadi kesalahan sistem, gagal mendapatakan data pasien dengan BPJS %v", err),
})
}
}
if patient.ID != 0 {
if patient.TanggalLahir.Before(value.TglLahir) {
@ -245,12 +252,14 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) {
errorMsg = append(errorMsg, map[string]any{
"no_rm": value.NoRekamMedis,
"status": "WARNING",
"message": fmt.Sprintf("No KartuBPJS ada yang sama dengan pasien: %s, mohon dicek kembali. current user: %v", patient.NoRm, value),
"message": fmt.Sprintf("No KartuBPJS ada yang sama dengan pasien dengan no rm yang ada di sistem: noRm: %s noBpjs: %s, mohon dicek kembali, NO BPJS pasien import: %s",
patient.NoRm, patient.NoKartuPesertaBPJS.String, value.NoBPJS.String,
),
})
}
}
err = repository.Store.ImportPatientTx(s.Store, r.Context(), value)
err = repository.Store.ImportPatientTx(s.Store, r.Context(), value, int32(currentUser.UserId), int32(currentUser.FasyankesId))
if err != nil {
errorMsg = append(errorMsg, map[string]any{
@ -274,12 +283,15 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) {
a := map[string]any{
"halo": "halo",
"file": "foo.txt",
// "contentType": contentType,
// "patient": tempArr,
"errorData": errorMsg,
// "errorData": errorMsg,
}
w.WriteHeader(200)
json.NewEncoder(w).Encode(a)
}

View File

@ -16,7 +16,7 @@ type Alamat struct {
Rw *string `json:"rw,omitempty"`
No_hp *string `json:"no_hp,omitempty"`
No_telpon *string `json:"no_telpon,omitempty"`
Pasien_id int32 `json:"pasen_id"`
Pasien_id int32 `json:"pasien_id"`
Status *bool `json:"status,omitempty"`
Created_at time.Time `json:"created_at"`
Updated_at time.Time `json:"updated_at"`

View File

@ -44,10 +44,13 @@ func StringToStringPtr(s string) *string {
return &s
}
func Pad(numStr string, size int32) string {
for int32(len(numStr)) < size {
numStr = "0" + numStr
func PadWithZeros(number int, zerosCount int) string {
numStr := strconv.Itoa(number)
padding := ""
for i := 0; i < zerosCount; i++ {
padding += "0"
}
return numStr
return padding + numStr
}