85 lines
2.9 KiB
Go
85 lines
2.9 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"primaya-api/cpone-middleware/internal/repository"
|
|
)
|
|
|
|
var labIdentifier = regexp.MustCompile(`^[A-Za-z0-9-]+$`)
|
|
|
|
func (h Handler) AddLabPackage(w http.ResponseWriter, r *http.Request) {
|
|
regID := strings.TrimSpace(r.PathValue("regId"))
|
|
if regID == "" || utf8.RuneCountInString(regID) > 50 || !labIdentifier.MatchString(regID) {
|
|
writeLabResponse(w, http.StatusUnprocessableEntity, false, "validation_error", "Nomor registrasi laboratorium hanya boleh berisi huruf, angka, dan tanda hubung", nil)
|
|
return
|
|
}
|
|
payload, _ := decodeJSONObject(r)
|
|
params, message := validateAddLabPackage(payload)
|
|
if message != "" {
|
|
writeLabResponse(w, http.StatusUnprocessableEntity, false, "validation_error", message, nil)
|
|
return
|
|
}
|
|
outcome, err := h.labRepository(r.Context()).AddLabPackage(r.Context(), strings.ToUpper(regID), params)
|
|
if err != nil {
|
|
if validationMessage, ok := asValidationError(err); ok {
|
|
writeLabResponse(w, http.StatusUnprocessableEntity, false, "validation_error", validationMessage, nil)
|
|
return
|
|
}
|
|
writeLabResponse(w, http.StatusInternalServerError, false, "error", err.Error(), nil)
|
|
return
|
|
}
|
|
writeLabResponse(w, outcome.HTTPStatus, outcome.Success, outcome.Status, outcome.Message, outcome.Data)
|
|
}
|
|
|
|
func validateAddLabPackage(payload map[string]any) (repository.AddLabPackageParams, string) {
|
|
packageID, message := requiredString(payload, "paket_id", 50, "Paket ID wajib diisi")
|
|
if message != "" {
|
|
return repository.AddLabPackageParams{}, message
|
|
}
|
|
packageID = strings.ToUpper(strings.TrimSpace(packageID))
|
|
if !labIdentifier.MatchString(packageID) {
|
|
return repository.AddLabPackageParams{}, "Paket ID hanya boleh berisi huruf, angka, dan tanda hubung"
|
|
}
|
|
cito := false
|
|
if value, present := nullableValue(payload, "cito"); present {
|
|
var valid bool
|
|
cito, valid = parseLaravelBoolean(value)
|
|
if !valid {
|
|
return repository.AddLabPackageParams{}, "The cito field must be true or false."
|
|
}
|
|
}
|
|
markup := 0.0
|
|
if value, present := nullableValue(payload, "markup_cito"); present {
|
|
var valid bool
|
|
markup, valid = parseNumeric(value)
|
|
if !valid {
|
|
return repository.AddLabPackageParams{}, "The markup cito must be a number."
|
|
}
|
|
if markup < 0 || markup > 100 {
|
|
return repository.AddLabPackageParams{}, "The markup cito must be between 0 and 100."
|
|
}
|
|
}
|
|
if !cito {
|
|
markup = 0
|
|
}
|
|
notes, _, message := optionalString(payload, "catatan", 500)
|
|
if message != "" {
|
|
return repository.AddLabPackageParams{}, message
|
|
}
|
|
channel, present, message := optionalString(payload, "channel", 50)
|
|
if message != "" {
|
|
return repository.AddLabPackageParams{}, message
|
|
}
|
|
if !present {
|
|
channel = "CPONE"
|
|
}
|
|
return repository.AddLabPackageParams{
|
|
PackageID: packageID, Cito: cito, MarkupCito: markup,
|
|
Notes: strings.TrimSpace(notes), Channel: strings.ToUpper(strings.TrimSpace(channel)),
|
|
}, ""
|
|
}
|