import { PromoNotes, PromoReads } from "../../../models/index.js";
import { genId } from "../../../misc/gen-id.js";
export async function readPromo(note, promo, user) {
    if (promo.expiresAt.getTime() <= Date.now() || promo.remainingCredits <= 0 || promo.userId === user.id || note.userId === user.id || user.isAdmin || user.isModerator || user.isBot) {
        return;
    }
    const readDay = new Date().toISOString().slice(0, 10);
    const result = await PromoReads.createQueryBuilder().insert().values({
        id: genId(),
        createdAt: new Date(),
        noteId: note.id,
        userId: user.id,
        readDay
    }).orIgnore().returning("id").execute();
    if (result.raw.length === 0) {
        return;
    }
    await PromoNotes.createQueryBuilder().update().set({
        remainingCredits: ()=>`"remainingCredits" - 1`
    }).where(`"noteId" = :noteId`, {
        noteId: note.id
    }).andWhere(`"remainingCredits" > 0`).execute();
}
