Files

565 lines
11 KiB
Go

package migrasi
import (
"api-poliklinik/internal/database"
"api-poliklinik/pkg/database/mongo"
connDatabase "api-poliklinik/pkg/database/satu_data"
"api-poliklinik/pkg/models/mongo/insertpatient"
_struct "api-poliklinik/pkg/models/struct"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
"log"
"net/http"
"os"
"strings"
"time"
)
func MigratePasienToFHIR(c *gin.Context) {
// 1. Ambil data dari PostgreSQL
db := database.New().GetDB("simrs")
simrs := connDatabase.NewDatabaseService(db)
dataPasien := simrs.GetPasien()
if dataPasien == nil || len(dataPasien) == 0 {
c.JSON(http.StatusNotFound, gin.H{
"status": "error",
"message": "Tidak ada data pasien untuk dimigrasi",
})
return
}
local := os.Getenv("MONGODB_DEV_LOCAL")
mongoDBConn := database.New(local).GetMongoDB()
if mongoDBConn == nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": "error",
"message": "Koneksi database MongoDB gagal",
})
return
}
mongoDB := mongo.NewDatabaseServiceMongo(mongoDBConn)
migratedCount := 0
failedCount := 0
var migratedIDs []string
for _, pasien := range dataPasien {
objectID := primitive.NewObjectID()
var given []string
var family string
var text string
namaSplit := strings.Split(pasien.Nama, " ")
if len(namaSplit) > 1 {
family = namaSplit[len(namaSplit)-1]
given = namaSplit[:len(namaSplit)-1]
text = pasien.Nama
} else if len(namaSplit) == 1 {
family = namaSplit[0]
given = []string{}
text = pasien.Nama
} else {
family = ""
given = []string{}
text = ""
}
gender := "unknown"
if strings.ToLower(pasien.JenisKelamin) == "l" {
gender = "male"
} else if strings.ToLower(pasien.JenisKelamin) == "p" {
gender = "female"
}
var fhirPasien = insertpatient.Patient{
ResourceType: "Patient",
ID: objectID,
Active: true,
}
// Mengisi properti Identifier
fhirPasien.Identifier = []_struct.Identifier{
{
Use: "usual",
System: "",
Value: pasien.ID,
Type: _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Version: "",
Code: "ID",
Display: "SIMRSID",
},
},
Text: "ID",
},
Period: _struct.Period{
Start: "",
End: "",
},
Assigner: _struct.Reference{
Reference: "",
Display: "",
},
},
{
Use: "usual",
System: "",
Value: pasien.Nomr,
Type: _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Version: "",
Code: "RM",
Display: "RekamMedik",
},
},
Text: "RM",
},
Period: _struct.Period{
Start: "",
End: "",
},
Assigner: _struct.Reference{
Reference: "",
Display: "",
},
}, {
Use: "usual",
System: "",
Value: pasien.NomorKartu,
Type: _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Version: "",
Code: "NK",
Display: "Nomor Kartu",
},
},
Text: "NK",
},
Period: _struct.Period{
Start: "",
End: "",
},
Assigner: _struct.Reference{
Reference: "",
Display: "",
},
},
{
Use: "usual",
System: "",
Value: pasien.Noktp,
Type: _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Version: "",
Code: "NIK",
Display: "Nomor Induk Kependudukan",
},
},
Text: "NIK",
},
Period: _struct.Period{
Start: "",
End: "",
},
Assigner: _struct.Reference{
Reference: "",
Display: "",
},
},
{
Use: "usual",
System: "",
Value: pasien.SIM,
Type: _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Version: "",
Code: "SIM",
Display: "Surat Ijin Mengemudi",
},
},
Text: "SIM",
},
Period: _struct.Period{
Start: "",
End: "",
},
Assigner: _struct.Reference{
Reference: "",
Display: "",
},
},
{
Use: "usual",
System: "",
Value: pasien.Paspor,
Type: _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Version: "",
Code: "Paspor",
Display: "Paspor",
},
},
Text: "Paspor",
},
Period: _struct.Period{
Start: "",
End: "",
},
Assigner: _struct.Reference{
Reference: "",
Display: "",
},
},
}
fhirPasien.Name = []_struct.HumanName{
{
Use: "usual",
Text: text,
Family: family,
Given: given,
Prefix: []string{pasien.Title},
Suffix: []string{""},
Period: _struct.Period{
Start: "",
End: "",
},
},
}
// Mengisi properti Telecom
fhirPasien.Telecom = []_struct.ContactPoint{
{
System: "Phone",
Value: pasien.TeleponHp,
Use: "mobile",
Rank: "1",
Period: _struct.Period{
Start: "",
End: "",
},
},
{
System: "Home",
Value: pasien.TeleponRumah,
Use: "Home",
Rank: "2",
Period: _struct.Period{
Start: "",
End: "",
},
},
{
System: "Home2",
Value: pasien.TeleponRumah2,
Use: "Home2",
Rank: "2",
Period: _struct.Period{
Start: "",
End: "",
},
},
{
System: "Office",
Value: pasien.TeleponKantor,
Use: "Office",
Rank: "3",
Period: _struct.Period{
Start: "",
End: "",
},
},
}
// Mengisi properti Gender
fhirPasien.Gender = gender
// Mengisi properti BirthDate
fhirPasien.BirthDate = pasien.Tanggallahir
// Mengisi properti Address
fhirPasien.Address = []_struct.Address{
{
Use: "home",
Type: "both",
Text: pasien.AlamatKTP,
Line: []string{pasien.AlamatKTP},
Village: pasien.Kelurahan,
District: pasien.Kecamatan,
City: pasien.Kota,
State: pasien.Provinsi,
PostalCode: "",
Country: "Indonesia",
Period: _struct.Period{
Start: "",
End: "",
},
Extension: []_struct.Extension{
{
URL: "",
Extension: []_struct.ExtensionDetail{
{
URL: "state",
ValueDisplay: pasien.Provinsi,
ValueCode: pasien.IDProvinsi,
}, {
URL: "city",
ValueDisplay: pasien.Kota,
ValueCode: pasien.Kota,
}, {
URL: "district",
ValueDisplay: pasien.Kecamatan,
ValueCode: pasien.Kecamatan,
},
{
URL: "village",
ValueDisplay: pasien.Kelurahan,
ValueCode: pasien.Kelurahan,
},
{
URL: "rw",
ValueDisplay: "",
ValueCode: "",
},
{
URL: "rw",
ValueDisplay: "",
ValueCode: "",
},
},
},
},
},
}
fhirPasien.MaritalStatus = _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Version: "",
Code: "",
Display: "",
UserSelected: false,
},
},
Text: "",
}
fhirPasien.Contact = []_struct.Contact{
{
Relationship: []_struct.CodeableConcept{
{
Coding: []_struct.Coding{
{
System: "",
Code: "",
Display: pasien.HubunganPenanggungjawab,
},
},
Text: "Hubungan Penanggung Jawab",
},
},
Name: _struct.HumanName{
Use: "usual",
Text: pasien.NamaAyah,
Family: "",
Prefix: []string{""},
Suffix: []string{""},
Period: _struct.Period{
Start: "",
End: "",
},
},
Telecom: []_struct.ContactPoint{
{
System: "phone",
Value: pasien.TeleponPenanggungjawab,
Use: "phone",
},
},
Address: _struct.Address{
Use: "Home",
Type: "",
Text: pasien.AlamatPenanggungjawab,
Line: []string{pasien.AlamatPenanggungjawab},
City: "",
Country: "",
},
Gender: "male",
Organization: _struct.Reference{
Reference: pasien.IDPendidikanAyah,
Display: pasien.PendidikanAyah,
},
Period: _struct.Period{
Start: "",
End: "",
},
}, {
Relationship: []_struct.CodeableConcept{
{
Coding: []_struct.Coding{
{
System: "",
Code: "",
Display: pasien.HubunganPenanggungjawab,
},
},
Text: "Hubungan Penanggung Jawab",
},
},
Name: _struct.HumanName{
Use: "usual",
Text: pasien.NamaIbu,
Family: "",
Prefix: []string{""},
Suffix: []string{""},
Period: _struct.Period{
Start: "",
End: "",
},
},
Telecom: []_struct.ContactPoint{
{
System: "phone",
Value: pasien.TeleponPenanggungjawab,
Use: "phone",
},
},
Address: _struct.Address{
Use: "Home",
Type: "",
Text: pasien.AlamatPenanggungjawab,
Line: []string{pasien.AlamatPenanggungjawab},
City: "",
Country: "",
},
Gender: "female",
Organization: _struct.Reference{
Reference: pasien.IDPendidikanIbu,
Display: pasien.PendidikanIbu,
},
Period: _struct.Period{
Start: "",
End: "",
},
},
}
fhirPasien.Communication = []_struct.Communication{
{
Language: _struct.CodeableConcept{
Coding: []_struct.Coding{
{
System: "",
Code: "",
Display: pasien.Komunikasi,
},
},
Text: "Bahasa" + pasien.Komunikasi,
},
},
}
// Mengisi properti Photo
fhirPasien.Photo = []_struct.Photo{
{
ContentType: "",
Language: "",
Data: "",
URL: "",
Size: 0,
Hash: 0,
Title: "",
Creation: "",
Height: 0,
Width: 0,
Frames: 0,
Duration: 0,
Pages: 0,
},
}
fhirPasien.Link = []_struct.Link{
{
Other: _struct.Reference{
Reference: "",
Display: "",
},
Type: "",
},
}
fhirPasien.CreatedAt = pasien.CreatedAt
fhirPasien.UpdatedAt = pasien.UpdatedAt
fhirPasien.Extension = []_struct.Extension{
{
URL: "",
Extension: []_struct.ExtensionDetail{
{
URL: "trible",
ValueDisplay: pasien.Suku,
ValueCode: "",
},
{
URL: "national",
ValueDisplay: "Warga" + pasien.Kebangsaan,
ValueCode: "",
},
{
URL: "disabilitas",
ValueDisplay: pasien.Disabiltas,
ValueCode: "",
},
{
URL: "education",
ValueDisplay: pasien.Pendidikan,
ValueCode: pasien.IDpendidikan,
},
{
URL: "religion",
ValueDisplay: pasien.Agama,
ValueCode: pasien.IDAgama,
},
},
},
}
err := mongoDB.InsertPatient(fhirPasien)
if err != nil {
log.Printf("Gagal migrasi data praktisi %s: %v", pasien.ID, err)
failedCount++
continue
}
migratedIDs = append(migratedIDs, objectID.Hex())
migratedCount++
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
"message": "Migrasi data praktisi ke FHIR berhasil",
"total": len(dataPasien),
"migrated": migratedCount,
"failed": failedCount,
"migrated_ids": migratedIDs,
"timestamp": time.Now().Format(time.RFC3339),
})
}