import { USE_INVITE_KEYS } from "../config"; import type { AllContext } from "../types"; import { has } from "./helpers"; export const isUsingInviteKeys = (c: AllContext): boolean => { return has(c.env, "INVITE_POOL") && USE_INVITE_KEYS; }; export const doesInviteKeyHaveValues = async (c: AllContext, inviteKey: string | undefined): Promise => { if (isUsingInviteKeys(c)) { if (inviteKey === undefined) return false; const value = await c.env.INVITE_POOL.get(inviteKey); // Key does not exist if (value === null) return false; // check the amount we have const amount: number = parseInt(value); // nans are failure if (isNaN(amount)) return false; // -1 means infinite invites if (amount === -1 || amount >= 1) return true; else return false; } return true; }; export const consumeInviteKey = async (c: AllContext, inviteKey: string | undefined) => { if (isUsingInviteKeys(c)) { if (inviteKey === undefined) return; const value = await c.env.INVITE_POOL.get(inviteKey); if (value === null) { console.error(`attempted to use invite key ${inviteKey} but is invalid`); return; } // check the amount we have const amount: number = parseInt(value); // handle NaN if (isNaN(amount)) { console.warn(`${inviteKey} has the value of ${value} which triggers NaN.`); return; } // -1 has infinite invites if (amount === -1) return; const newValue: number = amount - 1; // Delete any keys that fall to 0, they should be removed from the db if (newValue <= 0) { await c.env.INVITE_POOL.delete(inviteKey); return; } // put the new value on the stack await c.env.INVITE_POOL.put(inviteKey, newValue.toString()); } };