import { GetLicenseStatusRequest, GetLicenseStatusResponse, LicenseStatus, LicenseActiveRequest, LicenseActiveResponse, } from './api-types.js'; import {prisma} from './db.js'; import pkg from '@prisma/client'; import type {LogContext} from '@rocicorp/logger'; const {LicenseStatus: PrismaLicenseStatus} = pkg; export async function getLicenseStatus( req: GetLicenseStatusRequest, ): Promise { const license = await prisma.license.findUnique({ where: {key: req.licenseKey}, }); const pleaseUpdate = false; if (!license) { return { status: LicenseStatus.Unknown, disable: false, pleaseUpdate, }; } switch (license.status) { case PrismaLicenseStatus.VALID: return {status: LicenseStatus.Valid, disable: false, pleaseUpdate}; case PrismaLicenseStatus.INVALID: return {status: LicenseStatus.Invalid, disable: false, pleaseUpdate}; default: throw new Error('Unknown license status'); } } export async function licenseActive( req: LicenseActiveRequest, lc: LogContext, ): Promise { lc = lc.withContext('licenseActive'); lc.debug?.('request', req); const license = await prisma.license.findUnique({ where: {key: req.licenseKey}, }); // TODO don't throw a 500 here, return a more sensible status. if (!license) { lc.error?.('license not found', req.licenseKey); throw new Error(`License not found`); } // Note: we record pings for any license we recognize, valid or not. // TODO if we only wrote one row per day per browser profile it would reduce // the size of this table. const active = await prisma.active.create({ data: { licenseKey: license.key, profileID: req.profileID, }, }); lc.debug?.('created active', active); return {}; }