update events

This commit is contained in:
andrusyakka
2023-08-01 16:48:54 +03:00
parent 46afe2e794
commit c55df47ada
5 changed files with 127 additions and 38 deletions

View File

@@ -11,6 +11,7 @@ type ServerConfig struct {
type MSHosts struct {
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"),
},
}
}

View File

@@ -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 err != nil {
return c.HTML(http.StatusBadGateway, err.Error())
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,
},
},
}
employee := usersMap[event.EmployeeId]
person, err = services.CreatePerson(newPerson, c.Request().Header)
if err != nil || person.ID.ID() == 0 {
return c.NoContent(http.StatusBadRequest)
}
}
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)

View File

@@ -2,7 +2,6 @@ package model
import (
"github.com/google/uuid"
"github.com/lib/pq"
"gorm.io/gorm"
"time"
)
@@ -14,23 +13,16 @@ type Event struct {
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[]"`
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"`
Person PersonalInformation `json:"person"`
MedicId uuid.UUID `json:"medic_id"`
}

View File

@@ -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"`
}

52
services/pd.go Normal file
View File

@@ -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
}