@@ -2,14 +2,10 @@ package postgres
22
33import (
44 "context"
5- "errors"
65
76 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 "
98
10- "github.com/code-payments/code-server/pkg/metrics"
11-
12- "github.com/code-payments/flipchat-server/database/prisma/db"
139 "github.com/code-payments/flipchat-server/intent"
1410)
1511
@@ -18,79 +14,32 @@ const (
1814)
1915
2016type store struct {
21- client * db. PrismaClient
17+ pool * pgxpool. Pool
2218}
2319
24- func NewInPostgres (client * db. PrismaClient ) intent.Store {
20+ func NewInPostgres (pool * pgxpool. Pool ) intent.Store {
2521 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 ,
3823 }
3924}
4025
4126func (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 )
6228}
6329
6430func (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 {
9033 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+ }
9439
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+ }
9645}
0 commit comments