|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/georgysavva/scany/v2/pgxscan" |
| 9 | + "github.com/jackc/pgx/v5/pgxpool" |
| 10 | + |
| 11 | + commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1" |
| 12 | + pushpb "github.com/code-payments/flipchat-protobuf-api/generated/go/push/v1" |
| 13 | + |
| 14 | + pg "github.com/code-payments/flipchat-server/database/postgres" |
| 15 | + "github.com/code-payments/flipchat-server/push" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + pushTokensTableName = "flipchat_pushtokens" |
| 20 | + allPushTokenFields = `"userId", "appInstallId", "token", "type", "createdAt", "updatedAt"` |
| 21 | +) |
| 22 | + |
| 23 | +type model struct { |
| 24 | + UserID string `db:"userId"` |
| 25 | + AppInstallID string `db:"appInstallId"` |
| 26 | + Token string `db:"token"` |
| 27 | + Type int `db:"type"` |
| 28 | + CreatedAt time.Time `db:"createdAt"` |
| 29 | + UpdatedAt time.Time `db:"updatedAt"` |
| 30 | +} |
| 31 | + |
| 32 | +func toModel(userID *commonpb.UserId, token push.Token) (*model, error) { |
| 33 | + return &model{ |
| 34 | + UserID: pg.Encode(userID.Value), |
| 35 | + AppInstallID: token.AppInstallID, |
| 36 | + Token: token.Token, |
| 37 | + Type: int(token.Type), |
| 38 | + }, nil |
| 39 | +} |
| 40 | + |
| 41 | +func fromModel(m *model) (push.Token, error) { |
| 42 | + return push.Token{ |
| 43 | + Type: pushpb.TokenType(m.Type), |
| 44 | + AppInstallID: m.AppInstallID, |
| 45 | + Token: m.Token, |
| 46 | + }, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (m *model) dbAdd(ctx context.Context, pool *pgxpool.Pool) error { |
| 50 | + query := `INSERT INTO ` + pushTokensTableName + ` (` + allPushTokenFields + `) VALUES ($1, $2, $3, $4, NOW(), NOW()) ON CONFLICT ("userId", "appInstallId") DO UPDATE SET "token" = $3, "updatedAt" = NOW() WHERE ` + pushTokensTableName + `."userId" = $1 AND ` + pushTokensTableName + `."appInstallId" = $2 RETURNING ` + allPushTokenFields |
| 51 | + return pgxscan.Get( |
| 52 | + ctx, |
| 53 | + pool, |
| 54 | + m, |
| 55 | + query, |
| 56 | + m.UserID, |
| 57 | + m.AppInstallID, |
| 58 | + m.Token, |
| 59 | + m.Type, |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +func dbGetTokensBatch(ctx context.Context, pool *pgxpool.Pool, userIDs ...*commonpb.UserId) ([]*model, error) { |
| 64 | + var res []*model |
| 65 | + |
| 66 | + queryParameters := make([]any, len(userIDs)) |
| 67 | + |
| 68 | + query := `SELECT ` + allPushTokenFields + ` FROM ` + pushTokensTableName + ` WHERE "userId" IN (` |
| 69 | + for i, userID := range userIDs { |
| 70 | + queryParameters[i] = pg.Encode(userID.Value) |
| 71 | + if i > 0 { |
| 72 | + query += fmt.Sprintf(",$%d", i+1) |
| 73 | + } else { |
| 74 | + query += fmt.Sprintf("$%d", i+1) |
| 75 | + } |
| 76 | + } |
| 77 | + query += ")" |
| 78 | + |
| 79 | + err := pgxscan.Select( |
| 80 | + ctx, |
| 81 | + pool, |
| 82 | + &res, |
| 83 | + query, |
| 84 | + queryParameters..., |
| 85 | + ) |
| 86 | + if err != nil { |
| 87 | + if pgxscan.NotFound(err) { |
| 88 | + return nil, nil |
| 89 | + } |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + return res, nil |
| 93 | +} |
| 94 | + |
| 95 | +func dbDeleteToken(ctx context.Context, pool *pgxpool.Pool, tokenType pushpb.TokenType, token string) error { |
| 96 | + query := `DELETE FROM ` + pushTokensTableName + ` WHERE "token" = $1 and "type" = $2` |
| 97 | + _, err := pool.Exec(ctx, query, token, tokenType) |
| 98 | + return err |
| 99 | +} |
| 100 | + |
| 101 | +func dbClearTokens(ctx context.Context, pool *pgxpool.Pool, userID *commonpb.UserId) error { |
| 102 | + query := `DELETE FROM ` + pushTokensTableName + ` WHERE "userId" = $1` |
| 103 | + _, err := pool.Exec(ctx, query, pg.Encode(userID.Value)) |
| 104 | + return err |
| 105 | +} |
0 commit comments