Skip to content

Commit dfe65cc

Browse files
committed
convert user admission to postgres stored function
1 parent 6c82d8b commit dfe65cc

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
exports.up = async function (knex) {
2+
return knex.schema
3+
.raw(
4+
`CREATE OR REPLACE FUNCTION admit_user(user_pubkey BYTEA, tos_accepted TIMESTAMP WITHOUT TIME ZONE)
5+
RETURNS INTEGER
6+
LANGUAGE plpgsql
7+
AS $$
8+
BEGIN
9+
PERFORM ASSERT_SERIALIZED();
10+
11+
INSERT INTO "users" ("pubkey", "is_admitted", "tos_accepted_at", "created_at", "updated_at")
12+
VALUES (user_pubkey, true, tos_accepted, now_utc(), now_utc())
13+
ON CONFLICT ("pubkey")
14+
DO UPDATE SET
15+
"is_admitted" = true,
16+
"tos_accepted_at" = tos_accepted,
17+
"updated_at" = now_utc();
18+
19+
RETURN 0;
20+
END;
21+
$$;`)
22+
}
23+
24+
exports.down = function (knex) {
25+
return knex.schema
26+
.raw('DROP FUNCTION IF EXISTS admit_user(BYTEA, TIMESTAMP WITHOUT TIME ZONE);')
27+
}

src/@types/repositories.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ export interface IUserRepository {
4343
findByPubkey(pubkey: Pubkey, client?: DatabaseClient): Promise<User | undefined>
4444
upsert(user: Partial<User>, client?: DatabaseClient): Promise<number>
4545
getBalanceByPubkey(pubkey: Pubkey, client?: DatabaseClient): Promise<bigint>
46+
admitUser(pubkey: Pubkey, admittedAt: Date, client?: DatabaseClient): Promise<void>
4647
}

src/repositories/user-repository.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,26 @@ export class UserRepository implements IUserRepository {
7878

7979
return BigInt(user.balance)
8080
}
81+
82+
public async admitUser(
83+
pubkey: Pubkey,
84+
admittedAt: Date,
85+
client: DatabaseClient = this.dbClient,
86+
): Promise<void> {
87+
debug('admit user: %s at %s', pubkey, admittedAt)
88+
89+
try {
90+
await client.raw(
91+
'select admit_user(?, ?)',
92+
[
93+
toBuffer(pubkey),
94+
admittedAt.toISOString(),
95+
]
96+
)
97+
} catch (error) {
98+
console.error('Unable to admit user. Reason:', error.message)
99+
100+
throw error
101+
}
102+
}
81103
}

src/services/payments-service.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,9 @@ export class PaymentsService implements IPaymentsService {
184184
&& amountPaidMsat >= admissionFeeAmount
185185
) {
186186
const date = new Date()
187-
// TODO: Convert to stored func
188-
await this.userRepository.upsert(
189-
{
190-
pubkey: invoice.pubkey,
191-
isAdmitted: true,
192-
tosAcceptedAt: date,
193-
updatedAt: date,
194-
},
187+
await this.userRepository.admitUser(
188+
invoice.pubkey,
189+
date,
195190
transaction.transaction,
196191
)
197192
}

0 commit comments

Comments
 (0)