Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions migrations/20260409201624_admit_user_func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
exports.up = async function (knex) {
return knex.schema
.raw(
`CREATE OR REPLACE FUNCTION admit_user(user_pubkey BYTEA, tos_accepted TIMESTAMP WITHOUT TIME ZONE)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM ASSERT_SERIALIZED();

INSERT INTO "users" ("pubkey", "is_admitted", "tos_accepted_at", "created_at", "updated_at")
VALUES (user_pubkey, true, tos_accepted, now_utc(), now_utc())
ON CONFLICT ("pubkey")
DO UPDATE SET
"is_admitted" = true,
"tos_accepted_at" = tos_accepted,
"updated_at" = now_utc();
Comment on lines +12 to +17

RETURN 0;
END;
$$;`)
}

exports.down = function (knex) {
return knex.schema
.raw('DROP FUNCTION IF EXISTS admit_user(BYTEA, TIMESTAMP WITHOUT TIME ZONE);')
}
1 change: 1 addition & 0 deletions src/@types/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ export interface IUserRepository {
findByPubkey(pubkey: Pubkey, client?: DatabaseClient): Promise<User | undefined>
upsert(user: Partial<User>, client?: DatabaseClient): Promise<number>
getBalanceByPubkey(pubkey: Pubkey, client?: DatabaseClient): Promise<bigint>
admitUser(pubkey: Pubkey, admittedAt: Date, client?: DatabaseClient): Promise<void>
}
22 changes: 22 additions & 0 deletions src/repositories/user-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,26 @@ export class UserRepository implements IUserRepository {

return BigInt(user.balance)
}

public async admitUser(
pubkey: Pubkey,
admittedAt: Date,
client: DatabaseClient = this.dbClient,
): Promise<void> {
debug('admit user: %s at %s', pubkey, admittedAt)

try {
await client.raw(
'select admit_user(?, ?)',
[
toBuffer(pubkey),
admittedAt.toISOString(),
]
)
} catch (error) {
console.error('Unable to admit user. Reason:', error.message)

throw error
}
}
}
11 changes: 3 additions & 8 deletions src/services/payments-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,9 @@ export class PaymentsService implements IPaymentsService {
&& amountPaidMsat >= admissionFeeAmount
) {
const date = new Date()
// TODO: Convert to stored func
await this.userRepository.upsert(
{
pubkey: invoice.pubkey,
isAdmitted: true,
tosAcceptedAt: date,
updatedAt: date,
},
await this.userRepository.admitUser(
invoice.pubkey,
date,
transaction.transaction,
)
}
Expand Down
Loading