Skip to content

Commit

Permalink
added method to account model that makes it easy to create an infinit…
Browse files Browse the repository at this point in the history
…e account key
  • Loading branch information
NoahSaso committed Jan 16, 2025
1 parent 0b57fe7 commit e84b5a1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/db/models/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ export class Account extends Model {
@HasMany(() => AccountCodeIdSet, 'accountPublicKey')
declare codeIdSets: AccountCodeIdSet[]

/**
* Adds a new infinite key to this account.
*/
public async addInfiniteKey({ name }: { name: string }) {
const { key: apiKey, hash: hashedKey } = AccountKey.generateKeyAndHash()

const accountKey = await this.$create<AccountKey>('key', {
name,
hashedKey,
})

await accountKey.$create<AccountKeyCredit>('credit', {
paymentSource: AccountKeyCreditPaymentSource.Manual,
paymentId: randomUUID(),
amount: '-1',
paidAt: new Date(),
})

return {
apiKey,
}
}

// Generates a random API key and creates a key on this account with it. Also
// setup one credit for the key to accept payment.
public async generateKey({
Expand Down

0 comments on commit e84b5a1

Please sign in to comment.