-
Notifications
You must be signed in to change notification settings - Fork 38
/
customer_support_remarks_deleter.go
67 lines (56 loc) · 2.1 KB
/
customer_support_remarks_deleter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package customersupportremarks
import (
"database/sql"
"errors"
"github.com/gofrs/uuid"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/db/utilities"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)
type customerSupportRemarkDeleter struct {
}
func NewCustomerSupportRemarkDeleter() services.CustomerSupportRemarkDeleter {
return &customerSupportRemarkDeleter{}
}
func (o customerSupportRemarkDeleter) DeleteCustomerSupportRemark(appCtx appcontext.AppContext, customerSupportRemarkID uuid.UUID) error {
var remark models.CustomerSupportRemark
err := appCtx.DB().Scope(utilities.ExcludeDeletedScope()).Find(&remark, customerSupportRemarkID)
if err != nil {
switch err {
case sql.ErrNoRows:
return apperror.NewNotFoundError(customerSupportRemarkID, "attempting to delete CustomerSupportRemark")
default:
return apperror.NewQueryError("CustomerSupportRemark", err, "")
}
}
/*
https://dp3.atlassian.net/browse/MB-12730
MB-12730 udpdates to customer support remarks are restricted to the original remark creator
*/
sessionUserID := appCtx.Session().OfficeUserID
if remark.OfficeUserID != sessionUserID {
appCtx.Logger().Warn("Customer Support Remarks may only be edited by the user who created them.", zap.String("Customer Support RemarkID", customerSupportRemarkID.String()))
return apperror.NewForbiddenError("Action not allowed")
}
transactionError := appCtx.NewTransaction(func(_ appcontext.AppContext) error {
err := utilities.SoftDestroy(appCtx.DB(), &remark)
if err != nil {
switch err.Error() {
case "error updating model":
return apperror.NewUnprocessableEntityError("while updating model")
case "this model does not have deleted_at field":
return apperror.NewPreconditionFailedError(remark.ID, errors.New("model or sub table missing deleted_at field"))
default:
return apperror.NewInternalServerError("failed attempt to soft delete model")
}
}
return nil
})
if transactionError != nil {
return transactionError
}
return nil
}