import { db } from "@/db/postgre.js";
import { Plan } from "@/models/entities/plan.js";

export const PlanRepository = db.getRepository(Plan).extend({
	pack(src: Plan["id"] | Plan) {
		return Promise.resolve().then(async () => {
			const plan =
				typeof src === "object" ? src : await this.findOneByOrFail({ id: src });

			return {
				id: plan.id,
				createdAt: plan.createdAt.toISOString(),
				updatedAt: plan.updatedAt?.toISOString() ?? null,
				name: plan.name,
				icon: plan.icon,
				description: plan.description,
			};
		});
	},

	packMany(plans: Plan[]) {
		return Promise.all(plans.map((x) => this.pack(x)));
	},
});
