Create function
(ctx *fiber.Ctx)
| 49 | |
| 50 | // Create function |
| 51 | func (teamEndpoint *TeamEndpoint) Create(ctx *fiber.Ctx) error { |
| 52 | me, _ := userEndpoint.CurrentUser(ctx) |
| 53 | if can := userEndpoint.Can(ctx, models.AdminRole); !can { |
| 54 | return ctx.Status(401).JSON(fiber.Map{ |
| 55 | "message": "You are not authorized to perform this action", |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | currentAccount, _ := userEndpoint.CurrentAccount(ctx) |
| 60 | |
| 61 | teams, err := teamService.FindBy(bson.M{"accountId": me.AccountID}) |
| 62 | if err != nil { |
| 63 | return ctx.Status(401).JSON(fiber.Map{ |
| 64 | "message": err.Error(), |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | if len(teams) >= models.MaxTeamsPerPlan(currentAccount.PlanType) { |
| 69 | return ctx.Status(401).JSON(fiber.Map{ |
| 70 | "message": "You have reached the maximum number of teams for your plan. Upgrade it to increase the number.", |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | var inputMap = make(map[string]interface{}) |
| 75 | ctx.BodyParser(&inputMap) |
| 76 | |
| 77 | v := validate.Map(inputMap) |
| 78 | v.StringRule("code", "string|required") |
| 79 | v.StringRule("name", "string|required") |
| 80 | |
| 81 | if !v.Validate() { |
| 82 | return ctx.Status(422).JSON(v.Errors) |
| 83 | } |
| 84 | team, err := teamService.Create(v.SafeData(), me.AccountID) |
| 85 | if err != nil { |
| 86 | return ctx.Status(422).JSON(fiber.Map{ |
| 87 | "message": err.Error(), |
| 88 | }) |
| 89 | } |
| 90 | showTeam := models.ShowTeamSerializer().Transform(team) |
| 91 | return ctx.JSON(showTeam) |
| 92 | } |
| 93 | |
| 94 | // Update function |
| 95 | func (teamEndpoint *TeamEndpoint) Update(ctx *fiber.Ctx) error { |
nothing calls this directly
no test coverage detected