import { BaseCommand, args, flags } from '@adonisjs/core/ace' import type { CommandOptions } from '@adonisjs/core/types/ace' import User from '#models/user' import AccessService from '#services/access_service' /** Gives a user a tier (a payment, by hand until one is taken on the site); a running one is extended. */ export default class AccessTier extends BaseCommand { static commandName = 'access:tier' static description = "Sets a user's tier, e.g. access:tier someone@example.com vip --days=365" static options: CommandOptions = { startApp: true } @args.string({ description: 'The user’s email' }) declare email: string @args.string({ description: 'Tier (config/access.ts)' }) declare tier: string @flags.number({ description: 'For how many days (without: no end); the same tier still running is extended' }) declare days?: number async run() { const user = await User.findBy('email', this.email) if (!user) { this.logger.error(`No user ${this.email}`) this.exitCode = 1 return } AccessService.grantTier(user, this.tier, this.days ?? null) await user.save() this.logger.info(`${user.email}: ${user.tier} ${user.tierUntil ? `until ${user.tierUntil.toISO()}` : 'without end'}`) } }