fix(merge_component): 🐛 fix data lost after applied from merge

Fixing data lost after pressed on apply button in merge modal, WIP selection merge
This commit is contained in:
pakintada@gmail.com 2024-03-19 10:44:25 +07:00
parent cd0f67bb44
commit 58746ebdfb
5 changed files with 182 additions and 50 deletions

View file

@ -438,7 +438,7 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
}
// cache to redis
d.redisClient.SetToKey(filename, recipe)
err = d.redisClient.SetToKey(filename, recipe)
if err != nil {
d.taoLogger.Log.Error("GetRecipe: Error when read recipe file, Return default recipe", zap.Error(err))
@ -491,6 +491,22 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
// return d.currentRecipe
// }
func loopMatchProductCode(recipe01s []models.Recipe01, productCode string) (models.Recipe01, error) {
for _, v := range recipe01s {
if v.ProductCode == productCode {
return v, nil
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
return subMenu, nil
}
}
}
}
return models.Recipe01{}, fmt.Errorf("NotFound")
}
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
// try convert
@ -513,33 +529,96 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
// //fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile)
// //fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
return v, nil
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
return subMenu, nil
}
}
// if redis online
if d.redisClient.HealthCheck() == nil {
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("useRedis", true))
recipeFromRedis := d.GetRecipe(countryID, filename)
// find productCode from this
// for _, v := range recipeFromRedis.Recipe01 {
// if v.ProductCode == productCode {
// return v, nil
// } else if len(v.SubMenu) > 0 {
// for _, subMenu := range v.SubMenu {
// if subMenu.ProductCode == productCode {
// return subMenu, nil
// }
// }
// }
// }
recipe01, err := loopMatchProductCode(recipeFromRedis.Recipe01, productCode)
if err == nil {
return recipe01, nil
}
} else {
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("useRedis", false))
// for _, v := range d.CurrentRecipe[countryID].Recipe01 {
// if v.ProductCode == productCode {
// return v, nil
// } else if len(v.SubMenu) > 0 {
// for _, subMenu := range v.SubMenu {
// if subMenu.ProductCode == productCode {
// return subMenu, nil
// }
// }
// }
// }
recipe01, err := loopMatchProductCode(d.CurrentRecipe[countryID].Recipe01, productCode)
if err == nil {
return recipe01, nil
}
}
// //fmt.Println("No result in current recipe", countryID)
} else if recipe, ok := d.recipeMap[filename]; ok {
// //fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile, recipe.Recipe[countryID], "countryID=", countryID)
for _, v := range recipe.Recipe[countryID].Recipe01 {
if v.ProductCode == productCode {
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
return v, nil
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
return subMenu, nil
}
}
// for _, v := range recipe.Recipe[countryID].Recipe01 {
// if v.ProductCode == productCode {
// // d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
// return v, nil
// } else if len(v.SubMenu) > 0 {
// for _, subMenu := range v.SubMenu {
// if subMenu.ProductCode == productCode {
// // d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
// return subMenu, nil
// }
// }
// }
// }
// if redis online
if d.redisClient.HealthCheck() == nil {
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("useRedis", true))
recipeFromRedis := d.GetRecipe(countryID, filename)
// find productCode from this
// for _, v := range recipeFromRedis.Recipe01 {
// if v.ProductCode == productCode {
// return v, nil
// } else if len(v.SubMenu) > 0 {
// for _, subMenu := range v.SubMenu {
// if subMenu.ProductCode == productCode {
// return subMenu, nil
// }
// }
// }
// }
recipe01, err := loopMatchProductCode(recipeFromRedis.Recipe01, productCode)
if err == nil {
return recipe01, nil
}
} else {
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("useRedis", false))
recipe01, err := loopMatchProductCode(recipe.Recipe[countryID].Recipe01, productCode)
if err == nil {
return recipe01, nil
}
}
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getFail", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
}
}
@ -601,18 +680,23 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
TimeStamps: time.Now().Unix(),
}
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
return v, nil
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", subMenu))
return subMenu, nil
}
}
}
// for _, v := range d.CurrentRecipe[countryID].Recipe01 {
// if v.ProductCode == productCode {
// // d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
// return v, nil
// } else if len(v.SubMenu) > 0 {
// for _, subMenu := range v.SubMenu {
// if subMenu.ProductCode == productCode {
// // d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", subMenu))
// return subMenu, nil
// }
// }
// }
// }
recipe01, err := loopMatchProductCode(d.CurrentRecipe[countryID].Recipe01, productCode)
if err == nil {
return recipe01, nil
}
return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode)
@ -1281,12 +1365,20 @@ func (d *Data) Merge(country string, filename string, attr string, changeKey str
return "UpdatedValueError", fmt.Errorf("updated value is nil")
}
// type assertion
updatedModel, ok := updated.(models.Recipe01)
d.taoLogger.Log.Debug("check on update model", zap.Any("appliedFromClient", updatedModel))
if !ok {
return d.MergeRecipeNoCache(country, filename, updatedModel)
}
var updateRecipe01 models.Recipe01
updatedRecord, _ := json.Marshal(updated)
json.Unmarshal(updatedRecord, &updateRecipe01)
return d.MergeRecipeNoCache(country, filename, updateRecipe01)
// // updatedModel, ok := updateRecipe01.(models.Recipe01)
// // d.taoLogger.Log.Debug("check on update model", zap.Any("ok?", ok), zap.Any("appliedFromClient", updatedModel))
// if ok {
// return d.MergeRecipeNoCache(country, filename, updatedModel)
// } else {
// d.taoLogger.Log.Debug("Merge", zap.Any("targetAssertionFail", updated))
// return "Fail to upgrade: NotMatchedByType", nil
// }
}
@ -1332,20 +1424,23 @@ func (d *Data) MergeRecipe(country, filename, changeKey string) (string, error)
// check address
fmt.Println("[Source] source === recipe? ", sourceRecipe == d.GetRecipe(country, filename))
// apply value to its source
d.SetValuesToRecipe(sourceRecipe.Recipe01, patchValue)
copyOfSourceRecipe := sourceRecipe
return d.finalizedVersion(country, sourceRecipe)
// apply value to its source
d.SetValuesToRecipe(copyOfSourceRecipe.Recipe01, patchValue)
return d.finalizedVersion(country, copyOfSourceRecipe)
}
func (d *Data) MergeRecipeNoCache(country string, filename string, updatedRecipe models.Recipe01) (string, error) {
// get recipe
sourceRecipe := d.GetRecipe(country, filename)
copyOfSourceRecipe := sourceRecipe
// apply value to its source
d.SetValuesToRecipe(sourceRecipe.Recipe01, updatedRecipe)
d.SetValuesToRecipe(copyOfSourceRecipe.Recipe01, updatedRecipe)
return d.finalizedVersion(country, sourceRecipe)
return d.finalizedVersion(country, copyOfSourceRecipe)
}
func (d *Data) finalizedVersion(country string, sourceRecipe *models.Recipe) (string, error) {