step 14 : add transaction when proses add doctor
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -947,369 +946,6 @@ func (ug *ServicesMdDoctor) GetMdDoctorByID(id string) (models.DoctorV1, error)
|
||||
}
|
||||
|
||||
// PROSES ADD DOCTOR
|
||||
func (su *ServicesMdDoctor) AddMdDoctorV0(inp models.DoctorV1) (models.DoctorV1, error) {
|
||||
logger, _ := zap.NewProduction()
|
||||
var data models.DoctorV1
|
||||
var datacek []models.DoctorV1
|
||||
|
||||
defer logger.Sync()
|
||||
logger.Error("VALUE YANG DI ADD",
|
||||
zap.Any("INPUTAN VALUE", inp),
|
||||
)
|
||||
|
||||
prm := "%" + strings.TrimSpace(inp.M_DoctorName) + "%"
|
||||
qryCek := `SELECT M_DoctorName
|
||||
FROM m_doctor
|
||||
WHERE M_DoctorName LIKE ?
|
||||
AND M_DoctorIsActive = 'Y'`
|
||||
err := dbx.Handlex.Select(&datacek, qryCek, prm)
|
||||
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error cek data by doctorname",
|
||||
zap.String("doctorname", inp.M_DoctorName),
|
||||
zap.Any("datacek", datacek),
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED")
|
||||
}
|
||||
if len(datacek) > 0 {
|
||||
defer logger.Sync()
|
||||
logger.Error("Doctor Name already taken by another data",
|
||||
zap.String("doctorname", inp.M_DoctorName),
|
||||
)
|
||||
return data, fmt.Errorf("doctor Name sudah dipakai")
|
||||
}
|
||||
|
||||
// VARIABLE START
|
||||
M_DoctorM_TitleID := 0
|
||||
M_DoctorCode := ""
|
||||
M_DoctorPrefix := ""
|
||||
M_DoctorPrefix2 := ""
|
||||
M_DoctorName := inp.M_DoctorName
|
||||
M_DoctorSuffix := ""
|
||||
M_DoctorSuffix2 := ""
|
||||
M_DoctorGender := inp.M_DoctorGender
|
||||
M_DoctorDOB := "0000-00-00 00:00:00"
|
||||
M_DoctorReligionCode := ""
|
||||
M_DoctorReligionSystem := ""
|
||||
M_DoctorBloodTypeCode := ""
|
||||
M_DoctorBloodTypeSystem := ""
|
||||
M_DoctorBloodRhCode := ""
|
||||
M_DoctorBloodRhSystem := ""
|
||||
M_DoctorEducationCode := ""
|
||||
M_DoctorEducationSystem := ""
|
||||
M_DoctorCitizenship := inp.M_DoctorCitizenship
|
||||
M_DoctorEtnicCode := ""
|
||||
M_DoctorEtnicSystem := ""
|
||||
M_DoctorIdentifierCode := ""
|
||||
M_DoctorIdentifierSystem := ""
|
||||
M_DoctorIdentifierValue := inp.M_DoctorIdentifierValue
|
||||
M_DoctorHp := ""
|
||||
M_DoctorEmail := ""
|
||||
M_DoctorS_SpecialistID := 0
|
||||
M_DoctorAddress := ""
|
||||
M_DoctorAddressRegionalCd := 0
|
||||
M_DoctorAddressCity := ""
|
||||
M_DoctorAddressDistrict := ""
|
||||
M_DoctorAddressState := ""
|
||||
M_DoctorAddressCountry := ""
|
||||
// VARIABLE END
|
||||
|
||||
// CHECK PARAMS
|
||||
if inp.M_DoctorM_TitleID != 0 {
|
||||
M_DoctorM_TitleID = inp.M_DoctorM_TitleID
|
||||
}
|
||||
|
||||
if inp.M_DoctorCode != "" {
|
||||
M_DoctorCode = inp.M_DoctorCode
|
||||
}
|
||||
|
||||
if inp.M_DoctorPrefix != "" {
|
||||
M_DoctorPrefix = inp.M_DoctorPrefix
|
||||
}
|
||||
|
||||
if inp.M_DoctorPrefix2 != "" {
|
||||
M_DoctorPrefix2 = inp.M_DoctorPrefix2
|
||||
}
|
||||
|
||||
if inp.M_DoctorSuffix != "" {
|
||||
M_DoctorSuffix = inp.M_DoctorSuffix
|
||||
}
|
||||
|
||||
if inp.M_DoctorSuffix2 != "" {
|
||||
M_DoctorSuffix2 = inp.M_DoctorSuffix2
|
||||
}
|
||||
|
||||
if inp.M_DoctorDOB != "" {
|
||||
M_DoctorDOB = inp.M_DoctorDOB
|
||||
}
|
||||
|
||||
if inp.M_DoctorReligionCode != "default" {
|
||||
M_DoctorReligionCode = inp.M_DoctorReligionCode
|
||||
M_DoctorReligionSystem = inp.M_DoctorReligionSystem
|
||||
}
|
||||
|
||||
if inp.M_DoctorBloodTypeCode != "default" {
|
||||
M_DoctorBloodTypeCode = inp.M_DoctorBloodTypeCode
|
||||
M_DoctorBloodTypeSystem = inp.M_DoctorBloodTypeSystem
|
||||
}
|
||||
|
||||
if inp.M_DoctorBloodRhCode != "default" {
|
||||
M_DoctorBloodRhCode = inp.M_DoctorBloodRhCode
|
||||
M_DoctorBloodRhSystem = inp.M_DoctorBloodRhSystem
|
||||
}
|
||||
|
||||
if inp.M_DoctorEducationCode != "default" {
|
||||
M_DoctorEducationCode = inp.M_DoctorEducationCode
|
||||
M_DoctorEducationSystem = inp.M_DoctorEducationSystem
|
||||
}
|
||||
|
||||
if inp.M_DoctorEtnicCode != "default" {
|
||||
M_DoctorEtnicCode = inp.M_DoctorEtnicCode
|
||||
M_DoctorEtnicSystem = inp.M_DoctorEtnicSystem
|
||||
}
|
||||
|
||||
if inp.M_DoctorIdentifierCode != "default" {
|
||||
M_DoctorIdentifierCode = inp.M_DoctorIdentifierCode
|
||||
M_DoctorIdentifierSystem = inp.M_DoctorIdentifierSystem
|
||||
}
|
||||
|
||||
if inp.M_DoctorHp != "" {
|
||||
M_DoctorHp = inp.M_DoctorHp
|
||||
}
|
||||
|
||||
if inp.M_DoctorEmail != "" {
|
||||
M_DoctorEmail = inp.M_DoctorEmail
|
||||
}
|
||||
|
||||
if inp.M_DoctorM_SpecialistID != "0" {
|
||||
M_DoctorS_SpecialistID, err = strconv.Atoi(inp.M_DoctorM_SpecialistID)
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error CONVERT DOCTOR SPECIALIS",
|
||||
zap.Any("M_SpecialistID", M_DoctorS_SpecialistID),
|
||||
)
|
||||
return data, fmt.Errorf("error CONVERT DOCTOR SPECIALIS")
|
||||
}
|
||||
}
|
||||
|
||||
if inp.M_DoctorAddressRegionalCd != 0 {
|
||||
M_DoctorAddressRegionalCd = inp.M_DoctorAddressRegionalCd
|
||||
M_DoctorAddress = inp.M_DoctorAddress
|
||||
M_DoctorAddressCity = inp.M_DoctorAddressCity
|
||||
M_DoctorAddressDistrict = inp.M_DoctorAddressDistrict
|
||||
M_DoctorAddressState = inp.M_DoctorAddressState
|
||||
M_DoctorAddressCountry = inp.M_DoctorAddressCountry
|
||||
}
|
||||
|
||||
// CHECK PARAMS
|
||||
query := `INSERT INTO m_doctor
|
||||
(
|
||||
M_DoctorM_TitleID,
|
||||
M_DoctorCode,
|
||||
M_DoctorPrefix,
|
||||
M_DoctorPrefix2,
|
||||
M_DoctorName,
|
||||
M_DoctorSuffix,
|
||||
M_DoctorSuffix2,
|
||||
M_DoctorGender,
|
||||
M_DoctorDOB,
|
||||
M_DoctorReligionCode,
|
||||
M_DoctorReligionSystem,
|
||||
M_DoctorBloodTypeCode,
|
||||
M_DoctorBloodTypeSystem,
|
||||
M_DoctorBloodRhCode,
|
||||
M_DoctorBloodRhSystem,
|
||||
M_DoctorEducationCode,
|
||||
M_DoctorEducationSystem,
|
||||
M_DoctorCitizenship,
|
||||
M_DoctorEtnicCode,
|
||||
M_DoctorEtnicSystem,
|
||||
M_DoctorIdentifierCode,
|
||||
M_DoctorIdentifierSystem,
|
||||
M_DoctorIdentifierValue,
|
||||
M_DoctorHp,
|
||||
M_DoctorEmail,
|
||||
M_DoctorM_SpecialistID,
|
||||
M_DoctorAddress,
|
||||
M_DoctorAddressRegionalCd,
|
||||
M_DoctorAddressCity,
|
||||
M_DoctorAddressDistrict,
|
||||
M_DoctorAddressState,
|
||||
M_DoctorAddressCountry,
|
||||
M_DoctorIsActive,
|
||||
M_DoctorCreatedUserID,
|
||||
M_DoctorCreated
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)`
|
||||
|
||||
rst := dbx.Handlex.MustExec(query,
|
||||
M_DoctorM_TitleID,
|
||||
M_DoctorCode,
|
||||
M_DoctorPrefix,
|
||||
M_DoctorPrefix2,
|
||||
M_DoctorName,
|
||||
M_DoctorSuffix,
|
||||
M_DoctorSuffix2,
|
||||
M_DoctorGender,
|
||||
M_DoctorDOB,
|
||||
M_DoctorReligionCode,
|
||||
M_DoctorReligionSystem,
|
||||
M_DoctorBloodTypeCode,
|
||||
M_DoctorBloodTypeSystem,
|
||||
M_DoctorBloodRhCode,
|
||||
M_DoctorBloodRhSystem,
|
||||
M_DoctorEducationCode,
|
||||
M_DoctorEducationSystem,
|
||||
M_DoctorCitizenship,
|
||||
M_DoctorEtnicCode,
|
||||
M_DoctorEtnicSystem,
|
||||
M_DoctorIdentifierCode,
|
||||
M_DoctorIdentifierSystem,
|
||||
M_DoctorIdentifierValue,
|
||||
M_DoctorHp,
|
||||
M_DoctorEmail,
|
||||
M_DoctorS_SpecialistID,
|
||||
M_DoctorAddress,
|
||||
M_DoctorAddressRegionalCd,
|
||||
M_DoctorAddressCity,
|
||||
M_DoctorAddressDistrict,
|
||||
M_DoctorAddressState,
|
||||
M_DoctorAddressCountry,
|
||||
"Y",
|
||||
"1",
|
||||
time.Now(),
|
||||
)
|
||||
insertedID, err := rst.LastInsertId()
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error Insert doctor",
|
||||
zap.Any("object doctoraddv1", inp),
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED LastInsertId")
|
||||
}
|
||||
s := strconv.Itoa(int(insertedID))
|
||||
|
||||
// json after start
|
||||
var dataJSON models.DoctorJSONv1
|
||||
qryJson := `select *
|
||||
from m_doctor
|
||||
WHERE M_DoctorIsActive = 'Y'
|
||||
AND M_DoctorID = ?`
|
||||
|
||||
err = dbx.HandlexLog.Get(&dataJSON, qryJson, insertedID)
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error get json by id",
|
||||
// zap.Any("object doctoraddv1", inp),
|
||||
zap.Any("ERROR MSG", err.Error()),
|
||||
zap.Any("id last insert", s),
|
||||
)
|
||||
dbx.Handlex.MustBegin().Rollback()
|
||||
return data, fmt.Errorf("QUERY_FAILED GET DATA JSON AFTER INSERT")
|
||||
}
|
||||
|
||||
// Mengubah slice dari struct menjadi JSON string
|
||||
jsonData, err := json.Marshal(dataJSON)
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error format JSON",
|
||||
// zap.Any("object doctoraddv1", inp),
|
||||
zap.Any("ERROR MSG", err.Error()),
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED ERROR CONVERT JSON")
|
||||
}
|
||||
|
||||
// Mengubah JSON byte slice menjadi string
|
||||
jsonStringInsert := string(jsonData)
|
||||
|
||||
qryInsertLog := `INSERT INTO cpone_log
|
||||
(
|
||||
Log_DoctorM_DoctorID,
|
||||
Log_DoctorDate,
|
||||
Log_DoctorCode,
|
||||
Log_DoctorJsonBefore,
|
||||
Log_DoctorJsonAfter,
|
||||
Log_DoctorUserID
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?
|
||||
)`
|
||||
|
||||
_, err = dbx.HandlexLog.Exec(qryInsertLog,
|
||||
insertedID,
|
||||
time.Now(),
|
||||
"ADD",
|
||||
"",
|
||||
jsonStringInsert,
|
||||
"1",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error inserting query log",
|
||||
zap.Error(err),
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED INSERT_LOG")
|
||||
}
|
||||
|
||||
// json after end
|
||||
data, err = su.GetMdDoctorByID(s)
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Error("Error get doctor by id",
|
||||
// zap.Any("object doctoraddv1", inp),
|
||||
zap.Any("ERROR MSG", err.Error()),
|
||||
zap.Any("id last insert", s),
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED GET DATA AFTER INSERT")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, error) {
|
||||
logger, _ := zap.NewProduction()
|
||||
var data models.DoctorV1
|
||||
@@ -1466,6 +1102,10 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
}
|
||||
|
||||
// CHECK PARAMS
|
||||
tx := dbx.Handlex.MustBegin()
|
||||
// defer tx.Rollback()
|
||||
txLog := dbx.HandlexLog.MustBegin()
|
||||
// defer txLog.Rollback()
|
||||
query := `INSERT INTO m_doctor
|
||||
(
|
||||
M_DoctorM_TitleID,
|
||||
@@ -1543,7 +1183,7 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
NOW()
|
||||
)`
|
||||
|
||||
rst := dbx.Handlex.MustExec(query,
|
||||
rst := tx.MustExec(query,
|
||||
M_DoctorM_TitleID,
|
||||
M_DoctorCode,
|
||||
M_DoctorPrefix,
|
||||
@@ -1581,6 +1221,7 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
)
|
||||
insertedID, err := rst.LastInsertId()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
defer logger.Sync()
|
||||
logger.Error("Error Insert doctor",
|
||||
zap.Any("object doctoraddv1", inp),
|
||||
@@ -1596,8 +1237,9 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
WHERE M_DoctorIsActive = 'Y'
|
||||
AND M_DoctorID = ?`
|
||||
|
||||
err = dbx.Handlex.Get(&dataJSON, qryJson, s)
|
||||
err = tx.Get(&dataJSON, qryJson, s)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
defer logger.Sync()
|
||||
logger.Error("Error get json by id",
|
||||
// zap.Any("object doctoraddv1", inp),
|
||||
@@ -1640,7 +1282,7 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
?
|
||||
)`
|
||||
|
||||
_, err = dbx.HandlexLog.Exec(qryInsertLog,
|
||||
_, err = txLog.Exec(qryInsertLog,
|
||||
insertedID,
|
||||
"ADD",
|
||||
"",
|
||||
@@ -1649,6 +1291,8 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
txLog.Rollback()
|
||||
defer logger.Sync()
|
||||
logger.Error("Error inserting query log",
|
||||
zap.Any("ERROR MSG INSERT LOG", err.Error()),
|
||||
@@ -1657,8 +1301,72 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
}
|
||||
|
||||
// insert cpone_log.log_doctor end
|
||||
data, err = su.GetMdDoctorByID(s)
|
||||
|
||||
err = tx.Get(&data, `SELECT
|
||||
ROW_NUMBER() OVER () AS nomor,
|
||||
d.M_DoctorID,
|
||||
d.M_DoctorM_TitleID,
|
||||
ifnull(d.M_DoctorCode,'') as M_DoctorCode,
|
||||
ifnull(d.M_DoctorPrefix,'') as M_DoctorPrefix,
|
||||
ifnull(d.M_DoctorPrefix2,'') as M_DoctorPrefix2,
|
||||
CONCAT(
|
||||
IFNULL(d.M_DoctorPrefix, ''),
|
||||
' ',
|
||||
IFNULL(d.M_DoctorPrefix2, ''),
|
||||
' ',
|
||||
IFNULL(d.M_DoctorName, ''),
|
||||
' ',
|
||||
IFNULL(d.M_DoctorSuffix, ''),
|
||||
' ',
|
||||
IFNULL(d.M_DoctorSuffix2, '')
|
||||
) as M_DoctorName,
|
||||
ifnull(d.M_DoctorSuffix,'') as M_DoctorSuffix,
|
||||
ifnull(d.M_DoctorSuffix2,'') as M_DoctorSuffix2,
|
||||
d.M_DoctorGender,
|
||||
d.M_DoctorDOB,
|
||||
d.M_DoctorReligionCode,
|
||||
d.M_DoctorReligionSystem,
|
||||
d.M_DoctorBloodTypeCode,
|
||||
d.M_DoctorBloodTypeSystem,
|
||||
d.M_DoctorBloodRhCode,
|
||||
d.M_DoctorBloodRhSystem,
|
||||
d.M_DoctorEducationCode,
|
||||
d.M_DoctorEducationSystem,
|
||||
d.M_DoctorCitizenship,
|
||||
d.M_DoctorEtnicCode,
|
||||
d.M_DoctorEtnicSystem,
|
||||
d.M_DoctorIdentifierCode,
|
||||
d.M_DoctorIdentifierSystem,
|
||||
d.M_DoctorIdentifierValue,
|
||||
d.M_DoctorHp,
|
||||
d.M_DoctorEmail,
|
||||
d.M_DoctorM_SpecialistID,
|
||||
IFNULL(spesialis.M_SpecialistID, 0) as M_SpecialistID,
|
||||
IFNULL(spesialis.M_SpecialistName, '') as M_SpecialistName,
|
||||
IFNULL(d.M_DoctorAddress, '') as M_DoctorAddress,
|
||||
IFNULL(title.M_TitleName, '') as M_TitleName,
|
||||
d.M_DoctorCreated,
|
||||
d.M_DoctorCreatedUserID,
|
||||
d.M_DoctorLastUpdated,
|
||||
d.M_DoctorLastUpdatedUserID,
|
||||
d.M_DoctorDeletedUserID,
|
||||
d.M_DoctorDeleted,
|
||||
d.M_DoctorIsActive
|
||||
FROM m_doctor as d
|
||||
-- join m_title
|
||||
LEFT JOIN m_title as title
|
||||
ON d.M_DoctorM_TitleID = title.M_TitleID
|
||||
AND title.M_TitleIsActive = 'Y'
|
||||
-- join m_specialist
|
||||
LEFT JOIN m_specialist as spesialis
|
||||
ON d.M_DoctorM_SpecialistID = spesialis.M_SpecialistID
|
||||
AND spesialis.M_SpecialistIsActive = 'Y'
|
||||
WHERE d.M_DoctorIsActive = 'Y' AND
|
||||
d.M_DoctorID = ?`, s)
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
// txLog.Rollback()
|
||||
defer logger.Sync()
|
||||
logger.Error("Error get doctor by id",
|
||||
// zap.Any("object doctoraddv1", inp),
|
||||
@@ -1667,5 +1375,20 @@ func (su *ServicesMdDoctor) AddMdDoctor(inp models.DoctorV1) (models.DoctorV1, e
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED GET DATA AFTER INSERT")
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
logger.Error("Error committing transaction",
|
||||
zap.Any("ERROR MSG", err.Error()),
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED COMMIT")
|
||||
}
|
||||
err = txLog.Commit()
|
||||
if err != nil {
|
||||
logger.Error("Error committing log transaction",
|
||||
zap.Any("ERROR MSG", err.Error()),
|
||||
)
|
||||
return data, fmt.Errorf("QUERY_FAILED COMMIT LOG")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user