|
1
|
|
|
package repositories |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"errors" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/NdoleStudio/httpsms/pkg/entities" |
|
9
|
|
|
"github.com/NdoleStudio/httpsms/pkg/telemetry" |
|
10
|
|
|
"github.com/palantir/stacktrace" |
|
11
|
|
|
"gorm.io/gorm" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
// gormIntegration3CxRepository is responsible for persisting entities.Integration3CX |
|
15
|
|
|
type gormIntegration3CxRepository struct { |
|
16
|
|
|
logger telemetry.Logger |
|
17
|
|
|
tracer telemetry.Tracer |
|
18
|
|
|
db *gorm.DB |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
// NewGormIntegration3CXRepository creates the GORM version of the Integration3CxRepository |
|
22
|
|
|
func NewGormIntegration3CXRepository( |
|
23
|
|
|
logger telemetry.Logger, |
|
24
|
|
|
tracer telemetry.Tracer, |
|
25
|
|
|
db *gorm.DB, |
|
26
|
|
|
) Integration3CxRepository { |
|
27
|
|
|
return &gormIntegration3CxRepository{ |
|
28
|
|
|
logger: logger.WithService(fmt.Sprintf("%T", &gormIntegration3CxRepository{})), |
|
29
|
|
|
tracer: tracer, |
|
30
|
|
|
db: db, |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
func (repository *gormIntegration3CxRepository) DeleteAllForUser(ctx context.Context, userID entities.UserID) error { |
|
35
|
|
|
ctx, span := repository.tracer.Start(ctx) |
|
36
|
|
|
defer span.End() |
|
37
|
|
|
|
|
38
|
|
|
if err := repository.db.WithContext(ctx).Where("user_id = ?", userID).Delete(&entities.Integration3CX{}).Error; err != nil { |
|
39
|
|
|
msg := fmt.Sprintf("cannot delete all [%T] for user with ID [%s]", &entities.Integration3CX{}, userID) |
|
40
|
|
|
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return nil |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Load an entities.Integration3CX based on the entities.UserID |
|
47
|
|
|
func (repository *gormIntegration3CxRepository) Load(ctx context.Context, userID entities.UserID) (*entities.Integration3CX, error) { |
|
48
|
|
|
ctx, span := repository.tracer.Start(ctx) |
|
49
|
|
|
defer span.End() |
|
50
|
|
|
|
|
51
|
|
|
integration := new(entities.Integration3CX) |
|
52
|
|
|
err := repository.db.WithContext(ctx).Where("user_id = ?", userID).First(&integration).Error |
|
53
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
54
|
|
|
msg := fmt.Sprintf("[3cx] integration for user [%s] does not exist", userID) |
|
55
|
|
|
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg)) |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if err != nil { |
|
59
|
|
|
msg := fmt.Sprintf("cannot load [3cx] integration for user [%s]", userID) |
|
60
|
|
|
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return integration, nil |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Save an entities.Integration3CX |
|
67
|
|
|
func (repository *gormIntegration3CxRepository) Save(ctx context.Context, integration *entities.Integration3CX) error { |
|
68
|
|
|
ctx, span := repository.tracer.Start(ctx) |
|
69
|
|
|
defer span.End() |
|
70
|
|
|
|
|
71
|
|
|
if err := repository.db.WithContext(ctx).Save(integration).Error; err != nil { |
|
72
|
|
|
msg := fmt.Sprintf("cannot save [%T] with ID [%s]", integration, integration.ID) |
|
73
|
|
|
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return nil |
|
77
|
|
|
} |
|
78
|
|
|
|