Skip to content

Commit

Permalink
Merge branch 'release/v0.0.96'
Browse files Browse the repository at this point in the history
* release/v0.0.96:
  fix: discord audit log
  feat: init commit
  feat: sync project member status
  • Loading branch information
namnhce committed May 9, 2023
2 parents 67ab10e + 88ff44a commit adcda24
Show file tree
Hide file tree
Showing 20 changed files with 369 additions and 25 deletions.
41 changes: 41 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,47 @@ const docTemplate = `{
}
}
},
"/cron-jobs/sync-project-member-status": {
"put": {
"description": "Sync project member status",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Project"
],
"summary": "Sync project member status",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/view.MessageResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
}
}
}
},
"/cronjobs/audits": {
"post": {
"description": "Sync audit info from Notion to database",
Expand Down
41 changes: 41 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,47 @@
}
}
},
"/cron-jobs/sync-project-member-status": {
"put": {
"description": "Sync project member status",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Project"
],
"summary": "Sync project member status",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/view.MessageResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
}
}
}
},
"/cronjobs/audits": {
"post": {
"description": "Sync audit info from Notion to database",
Expand Down
27 changes: 27 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4525,6 +4525,33 @@ paths:
summary: Update client by id
tags:
- Client
/cron-jobs/sync-project-member-status:
put:
consumes:
- application/json
description: Sync project member status
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/view.MessageResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/view.ErrorResponse'
"404":
description: Not Found
schema:
$ref: '#/definitions/view.ErrorResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/view.ErrorResponse'
summary: Sync project member status
tags:
- Project
/cronjobs/audits:
post:
consumes:
Expand Down
13 changes: 13 additions & 0 deletions migrations/schemas/20230509095554-add_discord_log_template.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- +migrate Up
create table if not EXISTS discord_log_templates (
id uuid PRIMARY KEY DEFAULT(uuid()),
deleted_at TIMESTAMP(6),
created_at TIMESTAMP(6) DEFAULT(NOW()),
updated_at TIMESTAMP(6) DEFAULT(NOW()),

type TEXT,
content TEXT
);

-- +migrate Down
drop table discord_log_templates;
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type Discord struct {

type DiscordWebhook struct {
Campfire string
AuditLog string
}

type DiscordID struct {
Expand Down Expand Up @@ -202,6 +203,7 @@ func Generate(v ENV) *Config {
Discord: Discord{
Webhooks: DiscordWebhook{
Campfire: v.GetString("DISCORD_WEBHOOK_CAMPFIRE"),
AuditLog: v.GetString("DISCORD_WEBHOOK_AUDIT"),
},
SecretToken: v.GetString("DISCORD_SECRET_TOKEN"),
IDs: DiscordID{
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/dwarvesf/fortress-api/pkg/config"
"github.com/dwarvesf/fortress-api/pkg/controller/auth"
"github.com/dwarvesf/fortress-api/pkg/controller/client"
"github.com/dwarvesf/fortress-api/pkg/controller/discord"
"github.com/dwarvesf/fortress-api/pkg/controller/employee"
"github.com/dwarvesf/fortress-api/pkg/controller/invoice"
"github.com/dwarvesf/fortress-api/pkg/logger"
Expand All @@ -17,6 +18,7 @@ type Controller struct {
Client client.IController
Employee employee.IController
Invoice invoice.IController
Discord discord.IController
}

func New(store *store.Store, repo store.DBRepo, service *service.Service, worker *worker.Worker, logger logger.Logger, cfg *config.Config) *Controller {
Expand All @@ -25,5 +27,6 @@ func New(store *store.Store, repo store.DBRepo, service *service.Service, worker
Client: client.New(store, repo, service, logger, cfg),
Employee: employee.New(store, repo, service, logger, cfg),
Invoice: invoice.New(store, repo, service, worker, logger, cfg),
Discord: discord.New(store, repo, service, logger, cfg),
}
}
79 changes: 79 additions & 0 deletions pkg/controller/discord/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package discord

import (
"fmt"
"strings"

"github.com/dwarvesf/fortress-api/pkg/config"
"github.com/dwarvesf/fortress-api/pkg/logger"
"github.com/dwarvesf/fortress-api/pkg/model"
"github.com/dwarvesf/fortress-api/pkg/service"
"github.com/dwarvesf/fortress-api/pkg/store"
)

type IController interface {
Log(in model.LogDiscordInput) error
}

type controller struct {
store *store.Store
service *service.Service
logger logger.Logger
config *config.Config
repo store.DBRepo
}

func New(store *store.Store, repo store.DBRepo, service *service.Service, logger logger.Logger, cfg *config.Config) IController {
return &controller{
store: store,
service: service,
logger: logger,
config: cfg,
repo: repo,
}
}

func (c *controller) Log(in model.LogDiscordInput) error {
// Get discord template
template, err := c.store.DiscordLogTemplate.GetTemplateByType(c.repo.DB(), in.Type)
if err != nil {
c.logger.Field("err", err.Error()).Warn("Get Discord Template failed")
return err
}

data := in.Data.(map[string]interface{})

// get employee_id in discord format if any
if employeeID, ok := data["employee_id"]; ok {
employee, err := c.store.Employee.One(c.repo.DB(), employeeID.(string), false)
if err != nil {
c.logger.Field("err", err.Error()).Warn("Get Employee failed")
return err
}
data["employee_id"] = employee.DisplayName
}

if updatedEmployeeID, ok := data["updated_employee_id"]; ok {
updatedEmployee, err := c.store.Employee.One(c.repo.DB(), updatedEmployeeID.(string), false)
if err != nil {
c.logger.Field("err", err.Error()).Warn("Get Employee failed")
return err
}
data["updated_employee_id"] = updatedEmployee.DisplayName
}

// Replace template
content := template.Content
for k, v := range data {
content = strings.ReplaceAll(content, fmt.Sprintf("{{ %s }}", k), fmt.Sprintf("%v", v))
}

// log discord
_, err = c.service.Discord.SendMessage(content, c.config.Discord.Webhooks.AuditLog)
if err != nil {
c.logger.Field("err", err.Error()).Warn("Log failed")
return err
}

return nil
}
34 changes: 34 additions & 0 deletions pkg/handler/employee/employee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package employee

import (
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -242,6 +243,20 @@ func (h *handler) UpdateEmployeeStatus(c *gin.Context) {
return
}

userID, _ := authutils.GetUserIDFromContext(c, h.config)

err = h.controller.Discord.Log(model.LogDiscordInput{
Type: "employee_update_working_status",
Data: map[string]interface{}{
"working_status": emp.WorkingStatus.String(),
"employee_id": userID,
"updated_employee_id": emp.ID.String(),
},
})
if err != nil {
l.Error(err, "failed to logs to discord")
}

c.JSON(http.StatusOK, view.CreateResponse[any](view.ToEmployeeData(emp), nil, nil, nil, ""))
}

Expand Down Expand Up @@ -721,5 +736,24 @@ func (h *handler) UpdateBaseSalary(c *gin.Context) {
return
}

userID, err := authutils.GetUserIDFromContext(c, h.config)
if err != nil {
c.JSON(http.StatusBadRequest, view.CreateResponse[any](nil, nil, err, nil, ""))
return
}

// update discord as audit log
err = h.controller.Discord.Log(model.LogDiscordInput{
Type: "employee_update_base_salary",
Data: map[string]interface{}{
"employee_id": userID,
"updated_employee_id": employeeID,
"new_salary": fmt.Sprintf("%v vnđ", req.PersonalAccountAmount+req.CompanyAccountAmount),
},
})
if err != nil {
l.Error(err, "failed to logs to discord")
}

c.JSON(http.StatusOK, view.CreateResponse[any](view.ToBaseSalary(emp), nil, nil, nil, ""))
}
15 changes: 14 additions & 1 deletion pkg/handler/invoice/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"
"time"

"github.com/gin-gonic/gin"

"github.com/dwarvesf/fortress-api/pkg/config"
"github.com/dwarvesf/fortress-api/pkg/controller"
invoiceCtrl "github.com/dwarvesf/fortress-api/pkg/controller/invoice"
Expand All @@ -16,7 +18,6 @@ import (
"github.com/dwarvesf/fortress-api/pkg/utils/authutils"
"github.com/dwarvesf/fortress-api/pkg/view"
"github.com/dwarvesf/fortress-api/pkg/worker"
"github.com/gin-gonic/gin"
)

type handler struct {
Expand Down Expand Up @@ -204,6 +205,18 @@ func (h *handler) Send(c *gin.Context) {
return
}

// send message to discord channel
err = h.controller.Discord.Log(model.LogDiscordInput{
Type: "invoice_send",
Data: map[string]interface{}{
"invoice_number": iv.Number,
"employee_id": userID,
},
})
if err != nil {
l.Error(err, "failed to log to discord")
}

c.JSON(http.StatusOK, view.CreateResponse[any](nil, nil, nil, nil, "ok"))
}

Expand Down
23 changes: 12 additions & 11 deletions pkg/handler/project/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ package project
import "github.com/gin-gonic/gin"

type IHandler interface {
Create(c *gin.Context)
List(c *gin.Context)
UpdateProjectStatus(c *gin.Context)
GetMembers(c *gin.Context)
UpdateMember(c *gin.Context)
ArchiveWorkUnit(c *gin.Context)
AssignMember(c *gin.Context)
UnassignMember(c *gin.Context)
Create(c *gin.Context)
CreateWorkUnit(c *gin.Context)
DeleteMember(c *gin.Context)
DeleteSlot(c *gin.Context)
Details(c *gin.Context)
UpdateGeneralInfo(c *gin.Context)
UpdateContactInfo(c *gin.Context)
GetMembers(c *gin.Context)
GetWorkUnits(c *gin.Context)
CreateWorkUnit(c *gin.Context)
UpdateWorkUnit(c *gin.Context)
ArchiveWorkUnit(c *gin.Context)
List(c *gin.Context)
SyncProjectMemberStatus(c *gin.Context)
UnarchiveWorkUnit(c *gin.Context)
UnassignMember(c *gin.Context)
UpdateContactInfo(c *gin.Context)
UpdateGeneralInfo(c *gin.Context)
UpdateMember(c *gin.Context)
UpdateProjectStatus(c *gin.Context)
UpdateSendingSurveyState(c *gin.Context)
UpdateWorkUnit(c *gin.Context)
UploadAvatar(c *gin.Context)
}
Loading

0 comments on commit adcda24

Please sign in to comment.