-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* release/v0.0.96: fix: discord audit log feat: init commit feat: sync project member status
- Loading branch information
Showing
20 changed files
with
369 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
migrations/schemas/20230509095554-add_discord_log_template.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.