From 3e6f91f0aba51c505303e9f5cfaea813abc5482b Mon Sep 17 00:00:00 2001 From: nochill Date: Fri, 23 Feb 2024 14:10:40 +0700 Subject: [PATCH] fix import patient --- README.md | 3 + internal/repository/alamat.repository.go | 9 +-- internal/repository/keluarga.repository.go | 2 +- internal/repository/patient.repository.go | 84 ++++++++++++++++++---- internal/repository/store.go | 2 +- internal/repository/wilayah.repository.go | 2 +- internal/rest/handler.go | 56 +++++++++------ model/alamat.go | 2 +- util/common.go | 11 +-- 9 files changed, 125 insertions(+), 46 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..91140ee --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/internal/repository/alamat.repository.go b/internal/repository/alamat.repository.go index 56a69ae..cbd79bc 100644 --- a/internal/repository/alamat.repository.go +++ b/internal/repository/alamat.repository.go @@ -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]) diff --git a/internal/repository/keluarga.repository.go b/internal/repository/keluarga.repository.go index d2ae1e7..8514d4c 100644 --- a/internal/repository/keluarga.repository.go +++ b/internal/repository/keluarga.repository.go @@ -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) ` diff --git a/internal/repository/patient.repository.go b/internal/repository/patient.repository.go index 3fe41d1..47f1eae 100644 --- a/internal/repository/patient.repository.go +++ b/internal/repository/patient.repository.go @@ -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,7 +366,9 @@ func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPas provinsi, err := q.FindWilayahByName(ctx, *params.Provinsi) if err != nil { - return err + if err != pgx.ErrNoRows { + return err + } } if provinsi.Id == 0 { @@ -336,6 +381,17 @@ func (store *SQLStore) ImportPatientTx(ctx context.Context, params ImportDataPas Provinsi: provinsi_id, Nama_jalan: ¶ms.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)) +} diff --git a/internal/repository/store.go b/internal/repository/store.go index 126c172..18f3259 100644 --- a/internal/repository/store.go +++ b/internal/repository/store.go @@ -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 { diff --git a/internal/repository/wilayah.repository.go b/internal/repository/wilayah.repository.go index 42e6a3b..65d7aa0 100644 --- a/internal/repository/wilayah.repository.go +++ b/internal/repository/wilayah.repository.go @@ -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) diff --git a/internal/rest/handler.go b/internal/rest/handler.go index f7a635b..2686c51 100644 --- a/internal/rest/handler.go +++ b/internal/rest/handler.go @@ -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,12 +140,14 @@ func (s *Server) ImportPatientHandler(w http.ResponseWriter, r *http.Request) { patientExist, err := repository.Store.FindPatientByNoRm(s.Store, r.Context(), value.NoRekamMedis) if err != nil { - errorMsg = append(errorMsg, map[string]any{ - "no_rm": value.NoRekamMedis, - "status": "CRITICAL", - "message": fmt.Sprintf("Terjadi kesalahan sistem, mencari data pasien %v", err), - }) - continue + if err != pgx.ErrNoRows { + errorMsg = append(errorMsg, map[string]any{ + "no_rm": value.NoRekamMedis, + "status": "CRITICAL", + "message": fmt.Sprintf("Terjadi kesalahan sistem, mencari data pasien %v", err), + }) + continue + } } if patientExist.NoRm != "" { @@ -173,11 +176,13 @@ 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 { - 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 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 { @@ -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,11 +217,13 @@ 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 { - 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 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 { @@ -243,14 +250,16 @@ 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), + "no_rm": value.NoRekamMedis, + "status": "WARNING", + "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) } diff --git a/model/alamat.go b/model/alamat.go index 9e8ab94..0d1814d 100644 --- a/model/alamat.go +++ b/model/alamat.go @@ -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"` diff --git a/util/common.go b/util/common.go index 0860bd2..b7eea1c 100644 --- a/util/common.go +++ b/util/common.go @@ -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 }