@@ -2,14 +2,10 @@ package postgres
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
7
6
commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1"
8
- pg "github.com/code-payments/flipchat-server/database/postgres "
7
+ "github.com/jackc/pgx/v5/pgxpool "
9
8
10
- "github.com/code-payments/code-server/pkg/metrics"
11
-
12
- "github.com/code-payments/flipchat-server/database/prisma/db"
13
9
"github.com/code-payments/flipchat-server/intent"
14
10
)
15
11
@@ -18,79 +14,32 @@ const (
18
14
)
19
15
20
16
type store struct {
21
- client * db. PrismaClient
17
+ pool * pgxpool. Pool
22
18
}
23
19
24
- func NewInPostgres (client * db. PrismaClient ) intent.Store {
20
+ func NewInPostgres (pool * pgxpool. Pool ) intent.Store {
25
21
return & store {
26
- client ,
27
- }
28
- }
29
-
30
- func (s * store ) reset () {
31
- ctx := context .Background ()
32
-
33
- intents := s .client .Intent .FindMany ().Delete ().Tx ()
34
-
35
- err := s .client .Prisma .Transaction (intents ).Exec (ctx )
36
- if err != nil {
37
- panic (err )
22
+ pool : pool ,
38
23
}
39
24
}
40
25
41
26
func (s * store ) IsFulfilled (ctx context.Context , id * commonpb.IntentId ) (bool , error ) {
42
- tracer := metrics .TraceMethodCall (ctx , metricsStructName , "GetChatID" )
43
- defer tracer .End ()
44
-
45
- res , err := func () (bool , error ) {
46
- encodedIntentID := pg .Encode (id .Value , pg .Base58 )
47
-
48
- intent , err := s .client .Intent .FindFirst (
49
- db .Intent .ID .Equals (encodedIntentID ),
50
- ).Exec (ctx )
51
-
52
- if errors .Is (err , db .ErrNotFound ) || intent == nil {
53
- return false , nil
54
- }
55
-
56
- return intent .IsFulfilled , nil
57
- }()
58
-
59
- tracer .OnError (err )
60
-
61
- return res , err
27
+ return dbIsFulfilled (ctx , s .pool , id )
62
28
}
63
29
64
30
func (s * store ) MarkFulfilled (ctx context.Context , id * commonpb.IntentId ) error {
65
- tracer := metrics .TraceMethodCall (ctx , metricsStructName , "MarkFulfilled" )
66
- defer tracer .End ()
67
-
68
- err := func () error {
69
- encodedIntentID := pg .Encode (id .Value , pg .Base58 )
70
-
71
- ok , err := s .IsFulfilled (ctx , id )
72
- if err != nil {
73
- return err
74
- }
75
-
76
- if ok {
77
- return intent .ErrAlreadyFulfilled
78
- }
79
-
80
- // Upsert the intent with the new fulfilled status
81
- _ , err = s .client .Intent .UpsertOne (
82
- db .Intent .ID .Equals (encodedIntentID ),
83
- ).Create (
84
- db .Intent .ID .Set (encodedIntentID ),
85
- db .Intent .IsFulfilled .Set (true ),
86
- ).Update (
87
- db .Intent .IsFulfilled .Set (true ),
88
- ).Exec (ctx )
89
-
31
+ isFulfilled , err := dbIsFulfilled (ctx , s .pool , id )
32
+ if err != nil {
90
33
return err
91
- }()
92
-
93
- tracer .OnError (err )
34
+ } else if isFulfilled {
35
+ return intent .ErrAlreadyFulfilled
36
+ }
37
+ return dbMarkFulfilled (ctx , s .pool , id )
38
+ }
94
39
95
- return err
40
+ func (s * store ) reset () {
41
+ _ , err := s .pool .Exec (context .Background (), "DELETE FROM " + intentsTableName )
42
+ if err != nil {
43
+ panic (err )
44
+ }
96
45
}
0 commit comments