Files
liqa/server/src/session/session.service.ts
T
agent 7235abdcef
CI / changes (pull_request) Successful in 6s
CI / server (pull_request) Failing after 34m29s
CI / client (pull_request) Successful in 3m0s
CI / image (pull_request) Skipped
refactor(logging): separate console and debug logger classes
- Preserve TraceLogger console formatting and add DebugLogger with DEBUG namespace output.
- Select the logger through LOG_MODE and route application call sites through the factory.
- Cover factory selection, console formatting, debug output and auth-log redaction.
2026-11-11 14:30:37 -05:00

144 lines
3.7 KiB
TypeScript

import { Injectable, OnApplicationBootstrap } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import type { Cookie } from "playwright";
import { LessThan, Repository } from "typeorm";
import {
PaginatedResult,
PaginationQueryDto,
} from "../common/dto/pagination.dto";
import { createAppLogger } from "../common/app-logger";
import { SessionEntity } from "./session.entity";
export type SessionOrderBy =
| "id"
| "sessionName"
| "status"
| "lastUsedAt"
| "createdAt"
| "updatedAt";
@Injectable()
export class SessionService implements OnApplicationBootstrap {
private readonly logger = createAppLogger(SessionService.name);
constructor(
@InjectRepository(SessionEntity)
private readonly repo: Repository<SessionEntity>,
) {}
async onApplicationBootstrap(): Promise<void> {
const result = await this.repo.update(
{ status: "open" },
{ status: "closed" },
);
if ((result.affected ?? 0) > 0) {
this.logger.log(
`${result.affected} open session(s) closed on startup (no Playwright context available)`,
);
}
}
async upsert(
sessionName: string,
token: string,
cookies: Cookie[],
localStorage: Record<string, string>,
): Promise<SessionEntity> {
const now = new Date();
const existing = await this.repo.findOneBy({ sessionName });
if (existing) {
existing.token = token;
existing.cookies = JSON.stringify(cookies);
existing.localStorage = JSON.stringify(localStorage);
existing.status = "open";
existing.lastUsedAt = now;
return this.repo.save(existing);
}
return this.repo.save(
this.repo.create({
sessionName,
token,
cookies: JSON.stringify(cookies),
localStorage: JSON.stringify(localStorage),
status: "open",
lastUsedAt: now,
}),
);
}
findBySessionName(sessionName: string): Promise<SessionEntity | null> {
return this.repo.findOneBy({ sessionName });
}
findById(id: string): Promise<SessionEntity | null> {
return this.repo.findOneBy({ id });
}
async markOpen(sessionName: string): Promise<void> {
await this.repo.update({ sessionName }, { status: "open" });
}
async markClosed(sessionName: string): Promise<void> {
await this.repo.update({ sessionName }, { status: "closed" });
}
async touchLastUsed(sessionName: string): Promise<void> {
await this.repo.update({ sessionName }, { lastUsedAt: new Date() });
}
findExpiredOpen(cutoff: Date): Promise<SessionEntity[]> {
return this.repo
.createQueryBuilder("s")
.where("s.status = :status", { status: "open" })
.andWhere("(s.lastUsedAt IS NULL OR s.lastUsedAt < :cutoff)", { cutoff })
.getMany();
}
findOldClosed(cutoff: Date): Promise<SessionEntity[]> {
return this.repo.find({
where: { status: "closed", updatedAt: LessThan(cutoff) },
});
}
async findAll(
query: PaginationQueryDto<SessionOrderBy> = {},
): Promise<
PaginatedResult<
Pick<
SessionEntity,
| "id"
| "sessionName"
| "status"
| "lastUsedAt"
| "createdAt"
| "updatedAt"
>
>
> {
const page = query.page ?? 1;
const limit = query.limit ?? 20;
const orderBy = query.orderBy ?? "id";
const orderDir = query.orderDir ?? "ASC";
const [data, total] = await this.repo.findAndCount({
select: [
"id",
"sessionName",
"status",
"lastUsedAt",
"createdAt",
"updatedAt",
],
order: { [orderBy]: orderDir },
skip: (page - 1) * limit,
take: limit,
});
return { data, total, page, limit };
}
async remove(id: string): Promise<void> {
await this.repo.delete(id);
}
}