tbc
This commit is contained in:
@@ -11,11 +11,11 @@ import (
|
||||
)
|
||||
|
||||
type CreateDto struct {
|
||||
EntityType_Code eru.EntityTypeCode `form:"entityType_code" validate:"required"`
|
||||
Ref_Id *uint `form:"ref_id" validate:"required"`
|
||||
Type_Code eru.UploadCode `form:"type_code" validate:"required"`
|
||||
EntityType_Code eru.EntityTypeCode `form:"entityType_code"`
|
||||
Ref_Id *uint `form:"ref_id"`
|
||||
Type_Code eru.UploadCode `form:"type_code"`
|
||||
Name string `form:"name"`
|
||||
Upload_Employee_Id *string `form:"upload_employee_id"`
|
||||
Upload_Employee_Id *uint `form:"upload_employee_id"`
|
||||
FilePath string `json:"-"`
|
||||
|
||||
File multipart.File `json:"-"`
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package upload
|
||||
|
||||
import "fmt"
|
||||
|
||||
type (
|
||||
UploadCode string
|
||||
EntityTypeCode string
|
||||
@@ -16,22 +18,32 @@ const (
|
||||
|
||||
ETCPerson EntityTypeCode = "person"
|
||||
ETCEncounter EntityTypeCode = "encounter"
|
||||
ETCMCU EntityTypeCode = "mcu"
|
||||
)
|
||||
|
||||
var validUploadCodesByEntity = map[EntityTypeCode][]UploadCode{
|
||||
ETCPerson: {
|
||||
UCPRN, UCPDL, UCPP, UCPFC, UCMIR,
|
||||
UCPRN, UCPDL, UCPP, UCPFC,
|
||||
},
|
||||
ETCEncounter: {
|
||||
UCSEP, UCSIPP,
|
||||
},
|
||||
ETCMCU: {
|
||||
UCMIR,
|
||||
},
|
||||
}
|
||||
|
||||
func IsValidUploadCode(entity EntityTypeCode, code UploadCode) bool {
|
||||
for _, c := range validUploadCodesByEntity[entity] {
|
||||
func IsValidUploadCode(entity EntityTypeCode, code UploadCode) (bool, string) {
|
||||
allowedCodes, ok := validUploadCodesByEntity[entity]
|
||||
if !ok {
|
||||
return false, fmt.Sprintf("unknown entityType_code: %s", entity)
|
||||
}
|
||||
|
||||
for _, c := range allowedCodes {
|
||||
if c == code {
|
||||
return true
|
||||
return true, ""
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
return false, fmt.Sprintf("invalid upload_code '%s' for entityType_code '%s'", code, entity)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
eru "simrs-vx/internal/domain/references/upload"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
d "github.com/karincake/dodol"
|
||||
rw "github.com/karincake/risoles"
|
||||
@@ -57,12 +56,13 @@ func (obj myBase) Create(w http.ResponseWriter, r *http.Request) {
|
||||
MimeType: header.Header.Get("Content-Type"),
|
||||
}
|
||||
|
||||
errs := validateCreate(dto)
|
||||
if len(errs) > 0 {
|
||||
dataFail := validateCreate(dto)
|
||||
if dataFail != "" {
|
||||
rw.DataResponse(w, nil, d.FieldError{
|
||||
Code: "data-validation-fail",
|
||||
Message: strings.Join(errs, "\n"),
|
||||
Message: dataFail,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
res, err := u.Create(dto)
|
||||
|
||||
@@ -4,17 +4,16 @@ import (
|
||||
e "simrs-vx/internal/domain/main-entities/file-attachment"
|
||||
)
|
||||
|
||||
func validateCreate(dto e.CreateDto) []string {
|
||||
var errs []string
|
||||
func validateCreate(dto e.CreateDto) string {
|
||||
|
||||
switch {
|
||||
case dto.EntityType_Code == "":
|
||||
errs = append(errs, "entityType_code is required")
|
||||
return "entityType_code is required"
|
||||
case dto.Ref_Id == nil:
|
||||
errs = append(errs, "ref_id is required")
|
||||
return "ref_id is required"
|
||||
case dto.Type_Code == "":
|
||||
errs = append(errs, "type_code is required")
|
||||
return "type_code is required"
|
||||
}
|
||||
|
||||
return errs
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -269,7 +269,7 @@ func SetRoutes() http.Handler {
|
||||
hc.RegCrud(r, "/v1/adm-employee-hist", admemployeehist.O)
|
||||
hc.RegCrud(r, "/v1/therapy-protocol", therapyprotocol.O)
|
||||
hc.RegCrud(r, "/v1/chemo-protocol", chemoprotocol.O)
|
||||
hc.RegCrud(r, "POST /upload", fileattachment.O)
|
||||
hc.RegCrud(r, "/v1/upload", fileattachment.O)
|
||||
|
||||
/******************** actor ********************/
|
||||
hc.RegCrud(r, "/v1/person", person.O)
|
||||
|
||||
@@ -42,8 +42,15 @@ func Create(input e.CreateDto) (*d.Data, error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if !eru.IsValidUploadCode(input.EntityType_Code, input.Type_Code) {
|
||||
return errors.New("invalid upload code")
|
||||
valid, msg := eru.IsValidUploadCode(input.EntityType_Code, input.Type_Code)
|
||||
if !valid {
|
||||
event.Status = "failed"
|
||||
event.ErrInfo = pl.ErrorInfo{
|
||||
Code: "invalid-code",
|
||||
Detail: msg,
|
||||
Raw: errors.New(msg),
|
||||
}
|
||||
return pl.SetLogError(&event, input)
|
||||
}
|
||||
|
||||
input.FilePath, err = uploadAndGenerateFileUrl(input, &event)
|
||||
@@ -238,8 +245,15 @@ func Update(input e.UpdateDto) (*d.Data, error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if !eru.IsValidUploadCode(input.EntityType_Code, input.Type_Code) {
|
||||
return errors.New("invalid upload code")
|
||||
valid, msg := eru.IsValidUploadCode(input.EntityType_Code, input.Type_Code)
|
||||
if !valid {
|
||||
event.Status = "failed"
|
||||
event.ErrInfo = pl.ErrorInfo{
|
||||
Code: "invalid-code",
|
||||
Detail: msg,
|
||||
Raw: errors.New(msg),
|
||||
}
|
||||
return pl.SetLogError(&event, input)
|
||||
}
|
||||
|
||||
if data, err = ReadDetailData(rdDto, &event, tx); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user