206 lines
5.5 KiB
Go
206 lines
5.5 KiB
Go
package PatientRelated
|
|
|
|
import (
|
|
"api-poliklinik/internal/database"
|
|
"api-poliklinik/pkg/database/mongo"
|
|
patient "api-poliklinik/pkg/models/mongo/insertpatient"
|
|
"api-poliklinik/pkg/models/mongo/person"
|
|
"api-poliklinik/pkg/models/mongo/relatedperson"
|
|
_struct "api-poliklinik/pkg/models/struct"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func addRekamMedikIdentifier(identifierList *[]_struct.Identifier, rekamMedikNumber string) {
|
|
identifier := _struct.Identifier{
|
|
Use: "usual",
|
|
Type: _struct.CodeableConcept{
|
|
Coding: []_struct.Coding{
|
|
{
|
|
Code: "RM",
|
|
Display: "RekamMedik",
|
|
UserSelected: true,
|
|
},
|
|
},
|
|
Text: "RM",
|
|
},
|
|
Value: rekamMedikNumber,
|
|
Period: _struct.Period{
|
|
Start: time.Now().Format("2006-01-02"),
|
|
},
|
|
Assigner: _struct.Reference{},
|
|
}
|
|
*identifierList = append(*identifierList, identifier)
|
|
}
|
|
|
|
func InsertPatientRelated(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_LOCAL")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
}
|
|
var payload struct {
|
|
PATIENT patient.Patient `json:"patient"`
|
|
RELATED relatedperson.RelatedPerson `json:"relatedPerson"`
|
|
PERSON person.Person `json:"person"`
|
|
}
|
|
if err := c.Bind(&payload); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
|
|
payload.PATIENT.ResourceType = "Patient"
|
|
payload.RELATED.ResourceType = "relatePerson"
|
|
payload.PERSON.ResourceType = "person"
|
|
|
|
rekamMedikNumber, err := mongoDB.GetNextRekamMedikNumber()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to generate rekamedik number"})
|
|
return
|
|
}
|
|
|
|
addRekamMedikIdentifier(&payload.PATIENT.Identifier, rekamMedikNumber)
|
|
|
|
addRekamMedikIdentifier(&payload.PERSON.Identifier, rekamMedikNumber)
|
|
|
|
relatedID, errCreate := mongoDB.CreateRelatedperson(payload.RELATED)
|
|
if errCreate != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errCreate.Error()})
|
|
return
|
|
}
|
|
|
|
patientID, errCreate := mongoDB.InsertPatientlagi(payload.PATIENT)
|
|
if errCreate != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errCreate.Error()})
|
|
return
|
|
}
|
|
personID, errCreate := mongoDB.InsertPERSON(payload.PERSON)
|
|
if errCreate != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": errCreate.Error()})
|
|
return
|
|
}
|
|
|
|
// assign ObjectID ke patient
|
|
payload.PATIENT.Link = []_struct.Link{
|
|
{
|
|
Other: _struct.Reference{
|
|
Reference: "RelatedPerson/" + relatedID.Hex(),
|
|
Display: "",
|
|
},
|
|
Type: "",
|
|
},
|
|
}
|
|
payload.PERSON.Link = []_struct.LinkTarget{
|
|
{
|
|
Target: _struct.Reference{
|
|
Reference: "Patient/" + patientID.Hex(),
|
|
Display: "",
|
|
},
|
|
Type: "",
|
|
},
|
|
}
|
|
|
|
payload.RELATED.Link = []_struct.Link{
|
|
{
|
|
Other: _struct.Reference{
|
|
Reference: "Person/" + personID.Hex(),
|
|
Display: "",
|
|
},
|
|
Type: "",
|
|
},
|
|
}
|
|
|
|
payload.RELATED.Patient = _struct.Reference{
|
|
Reference: "Patient/" + patientID.Hex(),
|
|
Display: "",
|
|
}
|
|
|
|
dateCreated := time.Now().Format("2006-01-02 15:04:05")
|
|
updateCreated := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
if len(payload.PATIENT.Extension) > 0 {
|
|
payload.PATIENT.Extension[0].Extension = append(payload.PATIENT.Extension[0].Extension,
|
|
_struct.ExtensionDetail{
|
|
URL: "dateCreated",
|
|
ValueDisplay: dateCreated,
|
|
ValueCode: "",
|
|
},
|
|
_struct.ExtensionDetail{
|
|
URL: "UpdateCreated",
|
|
ValueDisplay: updateCreated,
|
|
ValueCode: "",
|
|
},
|
|
)
|
|
}
|
|
|
|
if len(payload.RELATED.Extension) > 0 {
|
|
payload.RELATED.Extension[0].Extension = append(payload.RELATED.Extension[0].Extension,
|
|
_struct.ExtensionDetail{
|
|
URL: "dateCreated",
|
|
ValueDisplay: dateCreated,
|
|
ValueCode: "",
|
|
},
|
|
_struct.ExtensionDetail{
|
|
URL: "UpdateCreated",
|
|
ValueDisplay: updateCreated,
|
|
ValueCode: "",
|
|
},
|
|
)
|
|
}
|
|
|
|
if len(payload.PERSON.Extension) > 0 {
|
|
payload.PERSON.Extension[0].Extension = append(payload.PERSON.Extension[0].Extension,
|
|
_struct.ExtensionDetail{
|
|
URL: "dateCreated",
|
|
ValueDisplay: dateCreated,
|
|
ValueCode: "",
|
|
},
|
|
_struct.ExtensionDetail{
|
|
URL: "UpdateCreated",
|
|
ValueDisplay: updateCreated,
|
|
ValueCode: "",
|
|
},
|
|
)
|
|
}
|
|
|
|
if err := mongoDB.UpdateIDpatient(patientID, payload.PATIENT); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to update patient with link"})
|
|
return
|
|
}
|
|
if err := mongoDB.UpdateIDrelated(relatedID, payload.RELATED); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to update relatedPerson with reference"})
|
|
return
|
|
}
|
|
|
|
if err := mongoDB.UpdateIDperson(personID, payload.PERSON); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to update relatedPerson with reference"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Patient ,RelatedPerson, Person Sukses di buat"})
|
|
}
|
|
|
|
func GetAllPatientdanRelated(c *gin.Context) {
|
|
local := os.Getenv("MONGODB_DEV_LOCAL")
|
|
db := database.New(local).GetMongoDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
|
|
limit := int64(0) // atau ambil dari query param
|
|
skip := int64(0)
|
|
|
|
mongoDB := mongo.NewDatabaseServiceMongo(db)
|
|
data, err := mongoDB.GetAllDataPatientWithRelated(limit, skip)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, data)
|
|
}
|