diff --git a/config/config.go b/config/config.go index 5329561..935642f 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,8 @@ type ServerConfig struct { } type MSHosts struct { - UsersHost string + UsersHost string + PersonsHost string } type Config struct { @@ -25,7 +26,8 @@ func New() *Config { ConnectionString: getEnv("CONNECTION_STRING", "host=localhost user=astra_events password=password dbname=astra_events_db port=5432 sslmode=disable TimeZone=Europe/Moscow"), }, MSHosts: MSHosts{ - UsersHost: getEnv("ASTRA-USERS", "http://localhost:8081"), + UsersHost: getEnv("ASTRA-USERS", "http://localhost:8083"), + PersonsHost: getEnv("ASTRA-PESONAL-INFORMATION", "http://localhost:8082"), }, } } diff --git a/handler/event.go b/handler/event.go index f01ca65..96af800 100644 --- a/handler/event.go +++ b/handler/event.go @@ -11,10 +11,10 @@ import ( ) func (h *Handler) CreateEvent(c echo.Context) (err error) { + var person *model.PersonalInformation event := new(model.CreateEvent) authorId, err := uuid.Parse(c.Request().Header.Get("UserId")) orgId, err := uuid.Parse(c.Request().Header.Get("UserOrganizationId")) - if err != nil { return c.NoContent(http.StatusUnauthorized) } @@ -23,19 +23,46 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) { return c.NoContent(http.StatusBadRequest) } - usersMap, err := services.GetUsers([]uuid.UUID{event.EmployeeId, authorId}, c.Request().Header) + if event.Person.ID.ID() != 0 { + person, err = services.GetPerson(event.Person.ID, c.Request().Header) + if err != nil || person.ID != event.Person.ID { + return c.NoContent(http.StatusBadRequest) + } + } else { + newPerson := model.PersonalInformationCreate{ + FirstName: event.Person.FirstName, + LastName: event.Person.LastName, + Patronymic: event.Person.Patronymic, + Contacts: []model.Contact{ + { + Category: "PHONE", + Value: event.Person.Phone, + }, + }, + } - if err != nil { - return c.HTML(http.StatusBadGateway, err.Error()) + person, err = services.CreatePerson(newPerson, c.Request().Header) + if err != nil || person.ID.ID() == 0 { + return c.NoContent(http.StatusBadRequest) + } } - employee := usersMap[event.EmployeeId] + usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, authorId}, c.Request().Header) + + if err != nil || len(usersMap) == 0 { + return c.HTML(http.StatusBadGateway, "Error get users") + } + + medic := usersMap[event.MedicId] author := usersMap[authorId] newEvent, err := utils.TypeConverter[model.Event](event) newEvent.OrgId = orgId + newEvent.MedicId = event.MedicId newEvent.AuthorId = authorId + newEvent.PersonId = person.ID + newEvent.Person = *person tx := h.DB.Create(&newEvent) @@ -44,7 +71,7 @@ func (h *Handler) CreateEvent(c echo.Context) (err error) { } newEvent.Author = author - newEvent.Employee = employee + newEvent.Medic = medic return c.JSON(http.StatusCreated, newEvent) } @@ -70,7 +97,7 @@ func (h *Handler) GetEvents(c echo.Context) (err error) { ids := set.New[uuid.UUID](len(events) * 2) for _, event := range events { - ids.Insert(event.EmployeeId) + ids.Insert(event.MedicId) ids.Insert(event.AuthorId) } @@ -81,7 +108,7 @@ func (h *Handler) GetEvents(c echo.Context) (err error) { for i, event := range events { events[i].Author = usersMap[event.AuthorId] - events[i].Employee = usersMap[event.EmployeeId] + events[i].Medic = usersMap[event.MedicId] } return c.JSON(http.StatusOK, events) @@ -102,16 +129,16 @@ func (h *Handler) GetEvent(c echo.Context) error { return c.NoContent(http.StatusNotFound) } - usersMap, err := services.GetUsers([]uuid.UUID{event.EmployeeId, event.AuthorId}, c.Request().Header) + usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, event.AuthorId}, c.Request().Header) if err != nil { return c.HTML(http.StatusBadGateway, err.Error()) } - employee := usersMap[event.EmployeeId] + medic := usersMap[event.MedicId] author := usersMap[event.AuthorId] - event.Employee = employee + event.Medic = medic event.Author = author return c.JSON(http.StatusOK, event) diff --git a/model/event.go b/model/event.go index 2c4de63..7a95f32 100644 --- a/model/event.go +++ b/model/event.go @@ -2,35 +2,27 @@ package model import ( "github.com/google/uuid" - "github.com/lib/pq" "gorm.io/gorm" "time" ) type Event struct { gorm.Model - ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primarykey"` - Start time.Time `json:"start"` - End time.Time `json:"end"` - Author User `json:"author" gorm:"-"` - AuthorId uuid.UUID `json:"-"` - Employee User `json:"employee" gorm:"-"` - EmployeeId uuid.UUID `json:"-"` - Member PD `json:"member" gorm:"-"` - MemberId uuid.UUID `json:"-"` - Status string `json:"status"` - MedicalCard Card `json:"medicalCard" gorm:"-"` - MedicalCardId uuid.UUID `json:"-"` - Services pq.Int32Array `json:"services" gorm:"type:integer[]"` - OrgId uuid.UUID `json:"-"` + ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primarykey"` + Start time.Time `json:"start"` + End time.Time `json:"end"` + Author User `json:"author" gorm:"-"` + AuthorId uuid.UUID `json:"-"` + Medic User `json:"medic" gorm:"-"` + MedicId uuid.UUID `json:"-"` + Person PersonalInformation `json:"person" gorm:"-"` + PersonId uuid.UUID `json:"-"` + OrgId uuid.UUID `json:"-"` } type CreateEvent struct { - Start time.Time `json:"start"` - End time.Time `json:"end"` - EmployeeId uuid.UUID `json:"employee_id"` - MemberId uuid.UUID `json:"member_id"` - Status string `json:"status"` - MedicalCardId uuid.UUID `json:"medical_card_id"` - Services []int32 `json:"services"` + Start time.Time `json:"start"` + End time.Time `json:"end"` + Person PersonalInformation `json:"person"` + MedicId uuid.UUID `json:"medic_id"` } diff --git a/model/pd.go b/model/pd.go index 9eda0ac..7584b96 100644 --- a/model/pd.go +++ b/model/pd.go @@ -1,7 +1,23 @@ package model -import "gorm.io/gorm" +import "github.com/google/uuid" -type PD struct { - gorm.Model +type PersonalInformation struct { + ID uuid.UUID `json:"id,omitempty"` + FirstName string `json:"first_name,omitempty"` + LastName string `json:"last_name,omitempty"` + Patronymic string `json:"patronymic,omitempty"` + Phone string `json:"phone,omitempty"` +} + +type PersonalInformationCreate struct { + FirstName string `json:"first_name,omitempty"` + LastName string `json:"last_name,omitempty"` + Patronymic string `json:"patronymic,omitempty"` + Contacts []Contact `json:"contacts"` +} + +type Contact struct { + Category string `json:"category"` + Value string `json:"value"` } diff --git a/services/pd.go b/services/pd.go new file mode 100644 index 0000000..5dd6238 --- /dev/null +++ b/services/pd.go @@ -0,0 +1,52 @@ +package services + +import ( + "astra-events/config" + "astra-events/model" + "astra-events/pkg/api" + "encoding/json" + "errors" + "github.com/google/uuid" + "net/http" +) + +func GetPerson(id uuid.UUID, header http.Header) (*model.PersonalInformation, error) { + data, err, _ := api.GetRequest( + config.Env.MSHosts.PersonsHost+"/persons/"+id.String(), + header) + + if err != nil { + return nil, errors.New("error Request to Personal Information Service") + } + + pi := new(model.PersonalInformation) + + err = json.Unmarshal(data, &pi) + + if err != nil { + return nil, errors.New("bad Response from Personal Information Service") + } + + return pi, nil +} + +func CreatePerson(person model.PersonalInformationCreate, header http.Header) (*model.PersonalInformation, error) { + data, err := api.PostRequest( + config.Env.MSHosts.PersonsHost+"/persons/", + person, + header, + ) + if err != nil { + return nil, errors.New("error Request to Personal Information Service") + } + + pi := new(model.PersonalInformation) + + err = json.Unmarshal(data, &pi) + + if err != nil { + return nil, errors.New("bad Response from Personal Information Service") + } + + return pi, nil +}