Prevent Mapfix Duplicates + Correctly Report Transaction Errors #221

Merged
Quaternions merged 3 commits from staging into master 2025-07-01 12:40:26 +00:00
4 changed files with 37 additions and 9 deletions

4
Cargo.lock generated
View File

@@ -1379,9 +1379,9 @@ dependencies = [
[[package]]
name = "rbx_asset"
version = "0.4.7"
version = "0.4.8"
source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
checksum = "6e55b4f6f15966cbb943bce20bcad32c2481f8d4d92a947749007b5bc517e1e1"
checksum = "8c067713a3201d0d2de3445ebecc8001952d914319863a9c7bc8f9212fdb1a17"
dependencies = [
"bytes",
"chrono",

View File

@@ -55,11 +55,16 @@ func (env *Mapfixes) Update(ctx context.Context, id int64, values datastore.Opti
// the update can only occur if the status matches one of the provided values.
func (env *Mapfixes) IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.MapfixStatus, values datastore.OptionalMap) error {
if err := env.db.Model(&model.Mapfix{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map()).Error; err != nil {
if err == gorm.ErrRecordNotFound {
result := env.db.Model(&model.Mapfix{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map())
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return datastore.ErrNotExist
}
return err
return result.Error
}
if result.RowsAffected == 0 {
return datastore.ErroNoRowsAffected
}
return nil

View File

@@ -55,11 +55,16 @@ func (env *Submissions) Update(ctx context.Context, id int64, values datastore.O
// the update can only occur if the status matches one of the provided values.
func (env *Submissions) IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.SubmissionStatus, values datastore.OptionalMap) error {
if err := env.db.Model(&model.Submission{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map()).Error; err != nil {
if err == gorm.ErrRecordNotFound {
result := env.db.Model(&model.Submission{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map())
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return datastore.ErrNotExist
}
return err
return result.Error
}
if result.RowsAffected == 0 {
return datastore.ErroNoRowsAffected
}
return nil

View File

@@ -35,7 +35,7 @@ var(
var (
ErrCreationPhaseMapfixesLimit = errors.New("Active mapfixes limited to 20")
ErrActiveMapfixSameTargetAssetID = errors.New("There is an active mapfix with the same TargetAssetID")
ErrActiveMapfixSameTargetAssetID = errors.New("There is an active mapfix for this map already")
ErrAcceptOwnMapfix = fmt.Errorf("%w: You cannot accept your own mapfix as the submitter", ErrPermissionDenied)
ErrCreateMapfixRateLimit = errors.New("You must not create more than 5 mapfixes every 10 minutes")
)
@@ -77,6 +77,24 @@ func (svc *Service) CreateMapfix(ctx context.Context, request *api.MapfixTrigger
}
}
// Check if a mapfix targetting the same map exists in creation phase
{
filter := datastore.Optional()
filter.Add("submitter", int64(userId))
filter.Add("target_asset_id", request.TargetAssetID)
filter.Add("status_id", CreationPhaseMapfixStatuses)
active_mapfixes, err := svc.DB.Mapfixes().List(ctx, filter, model.Page{
Number: 1,
Size: 1,
},datastore.ListSortDisabled)
if err != nil {
return nil, err
}
if len(active_mapfixes) != 0{
return nil, ErrActiveMapfixSameTargetAssetID
}
}
// Check if TargetAssetID actually exists
{
_, err := svc.Maps.Get(ctx, &maps.IdMessage{