Skip to content

Commit fedd1f8

Browse files
committed
[PSL-1188] implement single file registration
1 parent e2918b1 commit fedd1f8

File tree

5 files changed

+95
-43
lines changed

5 files changed

+95
-43
lines changed

common/storage/ticketstore/activation_attempts.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,23 @@ import (
55
)
66

77
type ActivationAttemptsQueries interface {
8-
UpsertActivationAttempt(attempt types.ActivationAttempt) (int64, error)
8+
InsertActivationAttempt(attempt types.ActivationAttempt) (int64, error)
9+
UpdateActivationAttempt(attempt types.ActivationAttempt) (int64, error)
910
GetActivationAttemptByID(id int) (*types.ActivationAttempt, error)
1011
GetActivationAttemptsByFileID(fileID string) ([]*types.ActivationAttempt, error)
1112
}
1213

13-
// UpsertActivationAttempt upsert a new activation attempt into the activation_attempts table
14-
func (s *TicketStore) UpsertActivationAttempt(attempt types.ActivationAttempt) (int64, error) {
15-
const upsertQuery = `
14+
// InsertActivationAttempt insert a new activation attempt into the activation_attempts table
15+
func (s *TicketStore) InsertActivationAttempt(attempt types.ActivationAttempt) (int64, error) {
16+
const insertQuery = `
1617
INSERT INTO activation_attempts (
17-
id, file_id, activation_attempt_at, is_successful, error_message
18-
) VALUES (?, ?, ?, ?, ?)
19-
ON CONFLICT(id)
20-
DO UPDATE SET
21-
file_id = excluded.file_id,
22-
activation_attempt_at = excluded.activation_attempt_at,
23-
is_successful = excluded.is_successful,
24-
error_message = excluded.error_message
25-
returning id;`
18+
file_id, activation_attempt_at, is_successful, error_message
19+
) VALUES (?, ?, ?, ?)
20+
RETURNING id;`
2621

2722
var id int64
28-
err := s.db.QueryRow(upsertQuery,
29-
attempt.ID, attempt.FileID, attempt.ActivationAttemptAt,
23+
err := s.db.QueryRow(insertQuery,
24+
attempt.FileID, attempt.ActivationAttemptAt,
3025
attempt.IsSuccessful, attempt.ErrorMessage).Scan(&id)
3126
if err != nil {
3227
return 0, err
@@ -35,6 +30,28 @@ func (s *TicketStore) UpsertActivationAttempt(attempt types.ActivationAttempt) (
3530
return id, nil
3631
}
3732

33+
// UpdateActivationAttempt update a new activation attempt into the activation_attempts table
34+
func (s *TicketStore) UpdateActivationAttempt(attempt types.ActivationAttempt) (int64, error) {
35+
const updateQuery = `
36+
UPDATE activation_attempts
37+
SET activation_attempt_at = ?, is_successful = ?, error_message = ?
38+
WHERE id = ? AND file_id = ?
39+
RETURNING id`
40+
41+
var id int64
42+
err := s.db.QueryRow(updateQuery,
43+
attempt.ActivationAttemptAt,
44+
attempt.IsSuccessful,
45+
attempt.ErrorMessage,
46+
attempt.ID,
47+
attempt.FileID).Scan(&id)
48+
if err != nil {
49+
return 0, err
50+
}
51+
52+
return id, nil
53+
}
54+
3855
// GetActivationAttemptByID retrieves an activation attempt by its ID from the activation_attempts table
3956
func (s *TicketStore) GetActivationAttemptByID(id int) (*types.ActivationAttempt, error) {
4057
const selectQuery = `

common/storage/ticketstore/registration_attempts.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,23 @@ import (
55
)
66

77
type RegistrationAttemptsQueries interface {
8-
UpsertRegistrationAttempt(attempt types.RegistrationAttempt) (int64, error)
8+
InsertRegistrationAttempt(attempt types.RegistrationAttempt) (int64, error)
9+
UpdateRegistrationAttempt(attempt types.RegistrationAttempt) (int64, error)
910
GetRegistrationAttemptByID(id int) (*types.RegistrationAttempt, error)
1011
GetRegistrationAttemptsByFileID(fileID string) ([]*types.RegistrationAttempt, error)
1112
}
1213

13-
// UpsertRegistrationAttempt upsert a new registration attempt into the registration_attempts table
14-
func (s *TicketStore) UpsertRegistrationAttempt(attempt types.RegistrationAttempt) (int64, error) {
15-
const upsertQuery = `
14+
// InsertRegistrationAttempt insert a new registration attempt into the registration_attempts table
15+
func (s *TicketStore) InsertRegistrationAttempt(attempt types.RegistrationAttempt) (int64, error) {
16+
const insertQuery = `
1617
INSERT INTO registration_attempts (
17-
id, file_id, reg_started_at, processor_sns, finished_at, is_successful, error_message
18-
) VALUES (?, ?, ?, ?, ?, ?, ?)
19-
ON CONFLICT(id)
20-
DO UPDATE SET
21-
file_id = COALESCE(excluded.file_id, registration_attempts.file_id),
22-
reg_started_at = COALESCE(excluded.reg_started_at, registration_attempts.reg_started_at),
23-
processor_sns = COALESCE(excluded.processor_sns, registration_attempts.processor_sns),
24-
finished_at = COALESCE(excluded.finished_at, registration_attempts.finished_at),
25-
is_successful = COALESCE(excluded.is_successful, registration_attempts.is_successful),
26-
error_message = COALESCE(excluded.error_message, registration_attempts.error_message)
27-
returning id;`
18+
file_id, reg_started_at, processor_sns, finished_at, is_successful, error_message
19+
) VALUES (?, ?, ?, ?, ?, ?)
20+
RETURNING id;`
2821

2922
var id int64
30-
err := s.db.QueryRow(upsertQuery,
31-
attempt.ID, attempt.FileID, attempt.RegStartedAt, attempt.ProcessorSNS,
23+
err := s.db.QueryRow(insertQuery,
24+
attempt.FileID, attempt.RegStartedAt, attempt.ProcessorSNS,
3225
attempt.FinishedAt, attempt.IsSuccessful, attempt.ErrorMessage).Scan(&id)
3326
if err != nil {
3427
return 0, err
@@ -37,6 +30,30 @@ func (s *TicketStore) UpsertRegistrationAttempt(attempt types.RegistrationAttemp
3730
return id, nil
3831
}
3932

33+
// UpdateRegistrationAttempt update a new registration attempt into the registration_attempts table
34+
func (s *TicketStore) UpdateRegistrationAttempt(attempt types.RegistrationAttempt) (int64, error) {
35+
const updateQuery = `
36+
UPDATE registration_attempts
37+
SET reg_started_at = ?,
38+
processor_sns = ?,
39+
finished_at = ?,
40+
is_successful = ?,
41+
error_message = ?
42+
WHERE id = ? AND file_id = ?
43+
RETURNING id;`
44+
45+
var id int64
46+
err := s.db.QueryRow(updateQuery,
47+
attempt.RegStartedAt, attempt.ProcessorSNS, attempt.FinishedAt,
48+
attempt.IsSuccessful, attempt.ErrorMessage,
49+
attempt.ID, attempt.FileID).Scan(&id)
50+
if err != nil {
51+
return 0, err
52+
}
53+
54+
return id, nil
55+
}
56+
4057
// GetRegistrationAttemptByID retrieves a registration attempt by its ID from the registration_attempts table
4158
func (s *TicketStore) GetRegistrationAttemptByID(id int) (*types.RegistrationAttempt, error) {
4259
const selectQuery = `

walletnode/api/services/cascade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (service *CascadeAPIHandler) StartProcessing(ctx context.Context, p *cascad
145145
case len(baseFileRegistrationAttempts) > maxFileRegistrationAttempts:
146146
return nil, cascade.MakeInternalServerError(errors.New("ticket registration attempts have been exceeded"))
147147
default:
148-
regAttemptID, err := service.register.UpsertRegistrationAttempts(types.RegistrationAttempt{
148+
regAttemptID, err := service.register.InsertRegistrationAttempts(types.RegistrationAttempt{
149149
FileID: p.FileID,
150150
RegStartedAt: time.Now().UTC(),
151151
})

walletnode/services/cascaderegister/service.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package cascaderegister
22

33
import (
44
"context"
5-
"github.com/google/uuid"
65
"time"
76

7+
"github.com/google/uuid"
8+
89
"github.com/pastelnetwork/gonode/common/storage/queries"
910
"github.com/pastelnetwork/gonode/common/storage/ticketstore"
1011
"github.com/pastelnetwork/gonode/common/types"
@@ -214,16 +215,32 @@ func (service *CascadeRegistrationService) GetActivationAttemptByID(attemptID in
214215
return actAttempt, nil
215216
}
216217

217-
func (service *CascadeRegistrationService) UpsertRegistrationAttempts(regAttempt types.RegistrationAttempt) (int64, error) {
218-
id, err := service.ticketDB.UpsertRegistrationAttempt(regAttempt)
218+
func (service *CascadeRegistrationService) InsertRegistrationAttempts(regAttempt types.RegistrationAttempt) (int64, error) {
219+
id, err := service.ticketDB.InsertRegistrationAttempt(regAttempt)
220+
if err != nil {
221+
return 0, err
222+
}
223+
return id, nil
224+
}
225+
226+
func (service *CascadeRegistrationService) UpdateRegistrationAttempts(regAttempt types.RegistrationAttempt) (int64, error) {
227+
id, err := service.ticketDB.UpdateRegistrationAttempt(regAttempt)
228+
if err != nil {
229+
return 0, err
230+
}
231+
return id, nil
232+
}
233+
234+
func (service *CascadeRegistrationService) InsertActivationAttempt(actAttempt types.ActivationAttempt) (int64, error) {
235+
id, err := service.ticketDB.InsertActivationAttempt(actAttempt)
219236
if err != nil {
220237
return 0, err
221238
}
222239
return id, nil
223240
}
224241

225-
func (service *CascadeRegistrationService) UpsertActivationAttempts(actAttempt types.ActivationAttempt) (int64, error) {
226-
id, err := service.ticketDB.UpsertActivationAttempt(actAttempt)
242+
func (service *CascadeRegistrationService) UpdateActivationAttempts(actAttempt types.ActivationAttempt) (int64, error) {
243+
id, err := service.ticketDB.UpdateActivationAttempt(actAttempt)
227244
if err != nil {
228245
return 0, err
229246
}

walletnode/services/cascaderegister/task.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package cascaderegister
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"time"
8+
69
"github.com/pastelnetwork/gonode/common/errgroup"
710
"github.com/pastelnetwork/gonode/common/types"
811
"github.com/pastelnetwork/gonode/walletnode/api/gen/nft"
9-
"os"
10-
"time"
1112

1213
"github.com/gabriel-vasile/mimetype"
1314
"github.com/pastelnetwork/gonode/common/errors"
@@ -99,7 +100,7 @@ func (task *CascadeRegistrationTask) run(ctx context.Context) error {
99100

100101
ra.FinishedAt = time.Now().UTC()
101102
ra.IsSuccessful = true
102-
_, err = task.service.UpsertRegistrationAttempts(*ra)
103+
_, err = task.service.UpdateRegistrationAttempts(*ra)
103104
if err != nil {
104105
log.Errorf("Error in registration attempts upsert: %v", err.Error())
105106
return err
@@ -112,7 +113,7 @@ func (task *CascadeRegistrationTask) run(ctx context.Context) error {
112113
}
113114

114115
actAttempt.IsSuccessful = true
115-
_, err = task.service.UpsertActivationAttempts(*actAttempt)
116+
_, err = task.service.UpdateActivationAttempts(*actAttempt)
116117
if err != nil {
117118
log.Errorf("Error in activation attempts upsert: %v", err.Error())
118119
return err
@@ -345,7 +346,7 @@ func (task *CascadeRegistrationTask) runTicketRegActTask(ctx context.Context) (r
345346

346347
log.WithContext(ctx).Info("Cascade Registrtion Ticket confirmed, now activating it..")
347348

348-
id, err := task.service.UpsertActivationAttempts(types.ActivationAttempt{
349+
id, err := task.service.InsertActivationAttempt(types.ActivationAttempt{
349350
FileID: task.Request.FileID,
350351
ActivationAttemptAt: time.Now().UTC(),
351352
})

0 commit comments

Comments
 (0)