mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
Merge branch 'feat/BE/sso-adjustment' into 'development'
[FIX/BE-US]add feature restrict by location and areas in roles See merge request mbugroup/lti-api!189
This commit is contained in:
@@ -108,6 +108,13 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var scopeErr error
|
||||
if params.ProjectFlockKandangId != 0 {
|
||||
if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), 0, params.ProjectFlockKandangId); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
}
|
||||
|
||||
limit := params.Limit
|
||||
if limit == 0 {
|
||||
limit = 10
|
||||
@@ -120,6 +127,10 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
|
||||
recordings, total, err := s.Repository.GetAll(c.Context(), offset, limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.Repository.WithRelations(db)
|
||||
db = db.
|
||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = recordings.project_flock_kandangs_id").
|
||||
Joins("JOIN project_flocks pf ON pf.id = pfk.project_flock_id")
|
||||
db, scopeErr = m.ApplyLocationScope(c, db, "pf.location_id")
|
||||
if params.ProjectFlockKandangId != 0 {
|
||||
db = db.Where("project_flock_kandangs_id = ?", params.ProjectFlockKandangId)
|
||||
}
|
||||
@@ -127,6 +138,9 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
return db.Order("recordings.record_datetime DESC").Order("recordings.created_at DESC")
|
||||
})
|
||||
|
||||
if scopeErr != nil {
|
||||
return nil, 0, scopeErr
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get recordings: %+v", err)
|
||||
return nil, 0, err
|
||||
@@ -141,6 +155,10 @@ func (s recordingService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
}
|
||||
|
||||
func (s recordingService) GetOne(c *fiber.Ctx, id uint) (*entity.Recording, error) {
|
||||
if err := m.EnsureRecordingAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
recording, err := s.Repository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
|
||||
return s.Repository.WithRelations(db)
|
||||
})
|
||||
@@ -164,6 +182,9 @@ func (s recordingService) GetNextDay(c *fiber.Ctx, projectFlockKandangId uint, r
|
||||
if projectFlockKandangId == 0 {
|
||||
return 0, fiber.NewError(fiber.StatusBadRequest, "project_flock_kandang_id is required")
|
||||
}
|
||||
if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), 0, projectFlockKandangId); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if recordTime.IsZero() {
|
||||
recordTime = time.Now().UTC()
|
||||
@@ -181,6 +202,9 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.EnsureProjectFlockKandangAccess(c, s.Repository.DB(), 0, req.ProjectFlockKandangId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := c.Context()
|
||||
recordTime := time.Now().UTC()
|
||||
@@ -352,6 +376,9 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.EnsureRecordingAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Stocks == nil && req.Depletions == nil && req.Eggs == nil {
|
||||
return s.GetOne(c, id)
|
||||
@@ -633,6 +660,11 @@ func (s recordingService) Approval(c *fiber.Ctx, req *validation.Approve) ([]ent
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, id := range req.ApprovableIds {
|
||||
if err := m.EnsureRecordingAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
actionValue := strings.ToUpper(strings.TrimSpace(req.Action))
|
||||
var action entity.ApprovalAction
|
||||
@@ -710,6 +742,9 @@ func (s recordingService) Approval(c *fiber.Ctx, req *validation.Approve) ([]ent
|
||||
}
|
||||
|
||||
func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := m.EnsureRecordingAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := c.Context()
|
||||
actorID, err := m.ActorIDFromContext(c)
|
||||
if err != nil {
|
||||
@@ -1445,6 +1480,7 @@ func ensureRecordingEggsUnused(eggs []entity.RecordingEgg) error {
|
||||
}
|
||||
|
||||
func stocksMatch(existing []entity.RecordingStock, incoming []validation.Stock) bool {
|
||||
|
||||
existingUsage := make(map[uint]float64)
|
||||
for _, stock := range existing {
|
||||
var usage float64
|
||||
|
||||
Reference in New Issue
Block a user