feat(deps): Add support for more departments

This commit is contained in:
pakintada@gmail.com 2024-06-19 13:56:56 +07:00
parent bdf2fac1ad
commit 4369ebb2a5
19 changed files with 296 additions and 169 deletions

View file

@ -1,44 +1,56 @@
[
{
"ignore": true,
"name": "New Country in full name",
"short": "Short name",
"permissions": "use number after 128, do not use 16 and 128"
},
{
"name": "Thailand",
"short": "tha",
"permissions": 1,
"dir": "tha"
},
{
"name": "Malaysia",
"short": "mys",
"permissions": 2,
"dir": "mys"
},
{
"name": "Australia",
"short": "aus",
"permissions": 4,
"dir": "aus"
},
{
"name": "Alpha3",
"short": "alpha-3",
"permissions": 8,
"dir": "alpha-3"
},
{
"name": "UAE Dubai",
"short": "uae-dubai",
"permissions": 256,
"dir": "dubai"
},
{
"name": "Counter Cafe",
"short": "counter",
"permissions": 512,
"dir": "counter"
}
]
{
"ignore": true,
"name": "New Country in full name",
"short": "Short name",
"permissions": "use number after 128, do not use 16 and 128"
},
{
"name": "Thailand",
"short": "tha",
"permissions": 1,
"dir": "tha"
},
{
"name": "Malaysia",
"short": "mys",
"permissions": 2,
"dir": "mys"
},
{
"name": "Australia",
"short": "aus",
"permissions": 4,
"dir": "aus"
},
{
"name": "Alpha3",
"short": "alpha-3",
"permissions": 8,
"dir": "alpha-3"
},
{
"name": "UAE Dubai",
"short": "uae-dubai",
"permissions": 256,
"dir": "dubai"
},
{
"name": "Counter Cafe",
"short": "counter",
"permissions": 512,
"dir": "counter01"
},
{
"name": "Singapore",
"short": "sgp",
"permissions": 1024,
"dir": "sgp"
},
{
"name": "Cocktail",
"short": "cocktail",
"permissions": 2048,
"dir": "cocktail_tha"
}
]

View file

@ -183,6 +183,22 @@ var (
CountryID: "aus",
CountryName: "Australia",
},
{
CountryID: "dubai",
CountryName: "UAE Dubai",
},
{
CountryID: "counter01",
CountryName: "Counter Cafe",
},
{
CountryID: "sgp",
CountryName: "Singapore",
},
{
CountryID: "cocktail_tha",
CountryName: "Cocktail",
},
}
)
@ -304,6 +320,8 @@ func NewData(taoLogger *logger.TaoLogger, redisClient *RedisCli) *Data {
// log.Panic("Error when read default recipe file:", err)
// }
fmt.Println(CurrentCountryIDMap)
return &Data{
CurrentFile: currentFileMap,
CurrentCountryID: CurrentCountryIDMap,
@ -1178,9 +1196,11 @@ func (d *Data) GetSubmenusOfRecipe(countryID, filename, productCode string) ([]m
func (d *Data) GetCountryNameByID(countryID string) (string, error) {
for _, country := range d.Countries {
if country.CountryID == countryID {
fmt.Println("Found " + countryID)
return country.CountryName, nil
}
}
fmt.Println("Not found " + countryID)
return "", fmt.Errorf("country ID: %s not found", countryID)
}

Binary file not shown.

View file

@ -2,7 +2,6 @@ package main
import (
"context"
"fmt"
"os/exec"
"time"
)
@ -31,7 +30,7 @@ func RunGitRecipeWorker() {
for {
err := pull_request()
if err != nil {
fmt.Println(err)
//fmt.Println(err)
}
time.Sleep(10 * time.Minute)
}

View file

@ -140,20 +140,20 @@ type CountryNamePerms struct {
func LoadCountrySettingsWithPermissions() []CountryNamePerms {
res := make([]CountryNamePerms, 0)
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
fmt.Println(path)
// path, err := os.Getwd()
// if err != nil {
// log.Println(err)
// }
// fmt.Println(path)
files, err := os.ReadDir(path)
if err != nil {
log.Fatalln(err)
}
// files, err := os.ReadDir(path)
// if err != nil {
// log.Fatalln(err)
// }
for _, file := range files {
fmt.Println(file.Name(), file.IsDir())
}
// for _, file := range files {
// fmt.Println(file.Name(), file.IsDir())
// }
// read file country.settings.json
content, err := os.Open("./country.settings.json")

View file

@ -88,6 +88,7 @@ func ValidatePermissions(p []permissions.Permission, nextRoute http.HandlerFunc)
for _, pm := range p {
if !u.Permissions.IsHavePermission(pm) {
// If not have permission response unauthorized
fmt.Println("tried access " + fmt.Sprintf("%v", pm))
w.WriteHeader(http.StatusUnauthorized)
err := json.NewEncoder(w).Encode("Unauthorized")
if err != nil {
@ -106,6 +107,7 @@ func ValidateOwnerOrPermissions(p []permissions.Permission, nextRoute http.Handl
reqUserID := chi.URLParam(r, "id")
u := r.Context().Value("user").(*models.User)
fmt.Println(u)
if reqUserID == "" {
// If not have permission response unauthorized
w.WriteHeader(http.StatusUnauthorized)

View file

@ -155,6 +155,7 @@ func (rr *RecipeRouter) Route(r chi.Router) {
for k := range rr.data.AllRecipeFiles {
countryName, err := rr.data.GetCountryNameByID(k)
if err != nil {
rr.taoLogger.Log.Error("RecipeRouter.Countries", zap.Any(countryName, err))
continue
}
keys = append(keys, countryName)
@ -854,7 +855,7 @@ func (rr *RecipeRouter) getImageOfProductCode(w http.ResponseWriter, r *http.Req
http.Error(w, err.Error(), http.StatusNotFound)
}
rr.taoLogger.Log.Debug("GetImageFromFast", zap.Int("Code", resp.StatusCode));
rr.taoLogger.Log.Debug("GetImageFromFast", zap.Int("Code", resp.StatusCode))
img_data, err := png.Decode(resp.Body)
if err != nil {
@ -864,7 +865,6 @@ func (rr *RecipeRouter) getImageOfProductCode(w http.ResponseWriter, r *http.Req
imgResult = img_data
// write image
png.Encode(w, imgResult)
}

View file

@ -34,6 +34,10 @@ func (ur *UserRouter) Route(r chi.Router) {
superPerm = superPerm | p.CountryPermission
}
if superPerm < 3999 {
superPerm = superPerm | permissions.Editor | permissions.Viewer
}
// Users
r.Route("/users", func(r chi.Router) {
r.Get("/", middlewares.ValidatePermissions([]permissions.Permission{permissions.Permission(superPerm)}, ur.getUsers))

View file

@ -100,6 +100,9 @@ func (s *Server) createHandler() {
for _, p := range perms {
superPerm = superPerm | p.CountryPermission
}
if superPerm < 3999 {
superPerm = superPerm | permissions.Editor | permissions.Viewer
}
// Seed
_ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "kenta420", "poomipat.c@forth.co.th", "", permissions.Permission(superPerm))

View file

@ -22,6 +22,7 @@ func (tl *TaoLogger) initConfig() *zap.Logger {
productionConfig := zap.NewProductionEncoderConfig()
productionConfig.EncodeTime = zapcore.ISO8601TimeEncoder
productionConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
// fileEncoder := zapcore.NewJSONEncoder(productionConfig)
consoleEncoder := zapcore.NewConsoleEncoder(productionConfig)