feat: fixing logic for approval ppid

This commit is contained in:
hanif salafi 2024-05-08 00:30:55 +07:00
parent e5775814d8
commit 3b05b665c3
8 changed files with 87 additions and 89 deletions

View File

@ -12,7 +12,7 @@ type PpidDataApprovalHistoriesCreateRequest struct {
PpidDataId uint `json:"ppidDataId" validate:"required"`
Message string `json:"message" validate:"required"`
ApprovalStatusId int `json:"approvalStatusId" validate:"required"`
ApprovalAtLevel int `json:"approvalAtLevel" validate:"required"`
ApprovalAtLevel *int `json:"approvalAtLevel"`
}
func (req PpidDataApprovalHistoriesCreateRequest) ToEntity() *entity.PpidDataApprovalHistories {
@ -20,6 +20,6 @@ func (req PpidDataApprovalHistoriesCreateRequest) ToEntity() *entity.PpidDataApp
PpidDataId: req.PpidDataId,
Message: req.Message,
ApprovalStatusId: req.ApprovalStatusId,
ApprovalAtLevel: req.ApprovalAtLevel,
ApprovalAtLevel: *req.ApprovalAtLevel,
}
}

View File

@ -169,14 +169,16 @@ func (_i *ppidDatasController) Update(c *fiber.Ctx) error {
// @Description API for UpdateApprovalStatus PpidDatas
// @Tags PPID Data
// @Security Bearer
// @Param Authorization header string true "Insert your access token" default(Bearer <Add access token here>)
// @Param payload body requestPpidApproval.PpidDataApprovalHistoriesCreateRequest true "Required payload"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.BadRequestError
// @Failure 401 {object} response.UnauthorizedError
// @Failure 500 {object} response.InternalServerError
// @Router /ppid-datas/approval [put]
// @Router /ppid-datas/approval [post]
func (_i *ppidDatasController) UpdateApprovalStatus(c *fiber.Ctx) error {
req := new(requestPpidApproval.PpidDataApprovalHistoriesCreateRequest)
if err := utilVal.ParseAndValidate(c, req); err != nil {
return err
}

View File

@ -48,7 +48,7 @@ func (_i *PpidDatasRouter) RegisterPpidDatasRoutes() {
router.Get("/:id", ppidDatasController.Show)
router.Post("/", ppidDatasController.Save)
router.Put("/:id", ppidDatasController.Update)
router.Put("/approval", ppidDatasController.UpdateApprovalStatus)
router.Post("/approval", ppidDatasController.UpdateApprovalStatus)
router.Delete("/:id", ppidDatasController.Delete)
})
}

View File

@ -1,7 +1,6 @@
package service
import (
"fmt"
"github.com/rs/zerolog"
"go-humas-be/app/database/entity"
requestPpidApproval "go-humas-be/app/module/ppid_data_approval_histories/request"
@ -121,6 +120,7 @@ func (_i *ppidDatasService) Update(id uint, req request.PpidDatasUpdateRequest)
}
func (_i *ppidDatasService) UpdateApprovalStatus(req requestPpidApproval.PpidDataApprovalHistoriesCreateRequest, authToken string) (err error) {
_i.Log.Info().Interface("data", req).Msg("")
ppidData, err := _i.Repo.FindOne(req.PpidDataId)
if err != nil {
return err
@ -135,37 +135,61 @@ func (_i *ppidDatasService) UpdateApprovalStatus(req requestPpidApproval.PpidDat
approvalBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
if approvalBy != nil {
findUserLevel, _ := _i.UserLevelsRepo.FindOne(uint(approvalBy.UserLevelId))
findUserParentLevel, _ := _i.UserLevelsRepo.FindOne(uint(findUserLevel.ParentLevelId))
if findUserLevel != nil {
if findUserLevel.LevelNumber == 1 {
isPublish := true
publishLevel := 1
timeNow := time.Now()
approvalByUserLevel, _ := _i.UserLevelsRepo.FindOne(uint(approvalBy.UserLevelId))
approvalByParentLevel, _ := _i.UserLevelsRepo.FindOne(uint(approvalByUserLevel.ParentLevelId))
_i.Log.Info().Interface("== findUserLevel ==", approvalByUserLevel).Msg("")
_i.Log.Info().Interface("== findUserParentLevel ==", approvalByParentLevel).Msg("")
if approvalByUserLevel != nil {
ppidData.ApprovalStatusId = req.ApprovalStatusId
if req.ApprovalStatusId == 1 {
if approvalByUserLevel.LevelNumber == 1 {
isPublish := true
publishLevel := 1
timeNow := time.Now()
ppidData.IsPublish = &isPublish
ppidData.PublishLevel = &publishLevel
ppidData.PublishedAt = &timeNow
ppidData.ApprovalStatusId = req.ApprovalStatusId
ppidData.NeedApprovalFromUserRole = nil
ppidData.NeedApprovalFromUserLevel = nil
} else if approvalByUserLevel.LevelNumber > 1 {
isPublish := true
publishLevel := 2
timeNow := time.Now()
ppidData.IsPublish = &isPublish
ppidData.PublishLevel = &publishLevel
ppidData.PublishedAt = &timeNow
needApprovalFromUserLevel := strconv.FormatUint(uint64(approvalByParentLevel.ID), 10)
ppidData.NeedApprovalFromUserLevel = &needApprovalFromUserLevel
findUserRoles, _ := _i.userRoleLevelDetailsRepo.FindByUserLevels(approvalByParentLevel.ID)
var needApprovalFromUserRole []string
for _, role := range findUserRoles {
roleId := strconv.FormatUint(uint64(role.UserRoleId), 10)
needApprovalFromUserRole = append(needApprovalFromUserRole, roleId)
}
needApprovalFromUserRoleStr := strings.Join(needApprovalFromUserRole, "/")
ppidData.NeedApprovalFromUserRole = &needApprovalFromUserRoleStr
}
} else if req.ApprovalStatusId == 2 {
createdBy, _ := _i.UsersRepo.FindOne(*ppidData.CreatedById)
if createdBy != nil {
needApprovalFromUserLevel := strconv.FormatUint(uint64(createdBy.UserLevelId), 10)
ppidData.NeedApprovalFromUserLevel = &needApprovalFromUserLevel
needApprovalFromUserRole := strconv.FormatUint(uint64(createdBy.UserRoleId), 10)
ppidData.NeedApprovalFromUserRole = &needApprovalFromUserRole
}
} else {
isPublish := false
ppidData.IsPublish = &isPublish
ppidData.PublishLevel = &publishLevel
ppidData.PublishedAt = &timeNow
ppidData.PublishLevel = nil
ppidData.PublishedAt = nil
ppidData.ApprovalStatusId = req.ApprovalStatusId
ppidData.NeedApprovalFromUserRole = nil
ppidData.NeedApprovalFromUserLevel = nil
} else if findUserLevel.LevelNumber == 2 {
isPublish := true
publishLevel := 2
timeNow := time.Now()
ppidData.IsPublish = &isPublish
ppidData.PublishLevel = &publishLevel
ppidData.PublishedAt = &timeNow
needApprovalFromUserLevel := fmt.Sprintf("%s", findUserParentLevel.ID)
ppidData.NeedApprovalFromUserLevel = &needApprovalFromUserLevel
findUserRoles, _ := _i.userRoleLevelDetailsRepo.FindByUserLevels(findUserParentLevel.ID)
var needApprovalFromUserRole []string
for _, role := range findUserRoles {
roleId := fmt.Sprintf("%s", role.UserRoleId)
needApprovalFromUserRole = append(needApprovalFromUserRole, roleId)
}
needApprovalFromUserRoleStr := strings.Join(needApprovalFromUserRole, "/")
ppidData.NeedApprovalFromUserRole = &needApprovalFromUserRoleStr
}
}
}

View File

@ -56,8 +56,7 @@ func (_i *userRoleLevelDetailsRepository) FindOne(id uint) (userRoleLevelDetails
}
func (_i *userRoleLevelDetailsRepository) FindByUserLevels(userLevelId uint) (userRoleLevelDetails []*entity.UserRoleLevelDetails, err error) {
if err := _i.DB.DB.Find(&userRoleLevelDetails).
Where(&entity.UserRoleLevelDetails{UserLevelId: userLevelId}).Error; err != nil {
if err := _i.DB.DB.Where(&entity.UserRoleLevelDetails{UserLevelId: userLevelId}).Find(&userRoleLevelDetails).Error; err != nil {
return nil, err
}

View File

@ -4637,7 +4637,7 @@ const docTemplate = `{
}
},
"/ppid-datas/approval": {
"put": {
"post": {
"security": [
{
"Bearer": []
@ -4649,6 +4649,14 @@ const docTemplate = `{
],
"summary": "UpdateApprovalStatus PpidDatas",
"parameters": [
{
"type": "string",
"default": "Bearer \u003cAdd access token here\u003e",
"description": "Insert your access token",
"name": "Authorization",
"in": "header",
"required": true
},
{
"description": "Required payload",
"name": "payload",
@ -6693,31 +6701,15 @@ const docTemplate = `{
"type": "object",
"required": [
"articleId",
"createdById",
"downloadCount",
"fileAlt",
"fileName",
"filePath",
"fileThumbnail",
"fileUrl",
"heightPixel",
"id",
"isPublish",
"publishedAt",
"size",
"statusId",
"widthPixel"
"statusId"
],
"properties": {
"articleId": {
"type": "integer"
},
"createdById": {
"type": "integer"
},
"downloadCount": {
"type": "integer"
},
"fileAlt": {
"type": "string"
},
@ -6950,7 +6942,6 @@ const docTemplate = `{
"request.PpidDataApprovalHistoriesCreateRequest": {
"type": "object",
"required": [
"approvalAtLevel",
"approvalStatusId",
"message",
"ppidDataId"

View File

@ -4626,7 +4626,7 @@
}
},
"/ppid-datas/approval": {
"put": {
"post": {
"security": [
{
"Bearer": []
@ -4638,6 +4638,14 @@
],
"summary": "UpdateApprovalStatus PpidDatas",
"parameters": [
{
"type": "string",
"default": "Bearer \u003cAdd access token here\u003e",
"description": "Insert your access token",
"name": "Authorization",
"in": "header",
"required": true
},
{
"description": "Required payload",
"name": "payload",
@ -6682,31 +6690,15 @@
"type": "object",
"required": [
"articleId",
"createdById",
"downloadCount",
"fileAlt",
"fileName",
"filePath",
"fileThumbnail",
"fileUrl",
"heightPixel",
"id",
"isPublish",
"publishedAt",
"size",
"statusId",
"widthPixel"
"statusId"
],
"properties": {
"articleId": {
"type": "integer"
},
"createdById": {
"type": "integer"
},
"downloadCount": {
"type": "integer"
},
"fileAlt": {
"type": "string"
},
@ -6939,7 +6931,6 @@
"request.PpidDataApprovalHistoriesCreateRequest": {
"type": "object",
"required": [
"approvalAtLevel",
"approvalStatusId",
"message",
"ppidDataId"

View File

@ -59,10 +59,6 @@ definitions:
properties:
articleId:
type: integer
createdById:
type: integer
downloadCount:
type: integer
fileAlt:
type: string
fileName:
@ -89,20 +85,10 @@ definitions:
type: string
required:
- articleId
- createdById
- downloadCount
- fileAlt
- fileName
- filePath
- fileThumbnail
- fileUrl
- heightPixel
- id
- isPublish
- publishedAt
- size
- statusId
- widthPixel
type: object
request.ArticlesCreateRequest:
properties:
@ -249,7 +235,6 @@ definitions:
ppidDataId:
type: integer
required:
- approvalAtLevel
- approvalStatusId
- message
- ppidDataId
@ -3655,9 +3640,15 @@ paths:
tags:
- PPID Data
/ppid-datas/approval:
put:
post:
description: API for UpdateApprovalStatus PpidDatas
parameters:
- default: Bearer <Add access token here>
description: Insert your access token
in: header
name: Authorization
required: true
type: string
- description: Required payload
in: body
name: payload