import { db } from "../../db/postgre.js";
import { UserGroup } from "../entities/user-group.js";
import { DriveFiles, UserGroupJoinings } from "../index.js";
export const UserGroupRepository = db.getRepository(UserGroup).extend({
    async pack (src) {
        const userGroup = typeof src === "object" ? src : await this.findOneByOrFail({
            id: src
        });
        const users = await UserGroupJoinings.findBy({
            userGroupId: userGroup.id
        });
        return {
            id: userGroup.id,
            createdAt: userGroup.createdAt.toISOString(),
            name: userGroup.name,
            username: userGroup.username,
            ownerId: userGroup.userId,
            allowCalls: userGroup.allowCalls,
            iconUrl: userGroup.iconFileId ? (await DriveFiles.pack(userGroup.iconFileId)).url : null,
            userIds: users.map((x)=>x.userId)
        };
    }
});
