{"size": 6232, "ext": "js", "max_stars_repo_path": "controllers/team/services.js", "max_stars_repo_name": "kien123456k/team-management-api", "max_stars_repo_stars_event_min_datetime": "2021-05-12T18:13:26.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-24T19:22:31.000Z", "max_issues_repo_path": "controllers/team/services.js", "max_issues_repo_name": "kien123456k/team-management-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/team/services.js", "max_forks_repo_name": "kien123456k/team-management-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 5, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 34.0546448087, "max_line_length": 90, "alphanum_fraction": 0.5425224647}
const {successResponseCreator} = require("../../helpers/response");

const Team = require("../../models/team");
const User = require("../../models/user");

const {MAX_TEAMS} = require("./schemas");

const objectIdReg = /^[0-9a-fA-F]{24}$/;

class TeamService {
    constructor(fastify) {
        this.fastify = fastify;
    }

    async create_team(user, teams, name, isPublished) {
        if (teams.length >= MAX_TEAMS) {
            throw this.fastify.httpErrors.badRequest(
                `A User cannot have more than ${MAX_TEAMS} teams`
            );
        }

        const existedTeam = await Team.findOne({name});
        if (existedTeam) {
            throw this.fastify.httpErrors.badRequest("Team's name is already exists");
        }

        const createdTeam = await Team.create({
            name,
            isPublished,
            creatorId: user._id,
        });
        if (!createdTeam) {
            throw this.fastify.httpErrors.internalServerError("Something went wrong");
        }

        user.teams.push(createdTeam._id);
        const savedUser = await user.save();
        if (!savedUser) {
            throw this.fastify.httpErrors.internalServerError("Something went wrong");
        }

        return successResponseCreator(201, "Create a team successfully", {
            name,
            description: "",
            avatarUrl: "",
            isPublished,
        });
    }

    async getAll_team(limit, offset, search) {
        let filteredTeams;
        if (search) {
            const regex = new RegExp(search);
            if (!limit || !offset) {
                filteredTeams = await Team.find({
                    $and: [{$or: [{name: regex}, {description: regex}]}],
                });
            } else {
                filteredTeams = await Team.find({
                    $and: [{$or: [{name: regex}, {description: regex}]}],
                })
                    .skip(offset * limit)
                    .limit(limit);
            }
        } else {
            if (!limit || !offset) {
                filteredTeams = await Team.find();
            } else {
                filteredTeams = await Team.find()
                    .skip(offset * limit)
                    .limit(limit);
            }
        }

        if (filteredTeams.length === 0) {
            return successResponseCreator(200, "Get teams successfully", []);
        }

        return successResponseCreator(200, "Get teams successfully", filteredTeams);
    }

    async get_team(teams, limit, offset, search) {
        let filteredTeams = teams;
        if (search) {
            filteredTeams = filteredTeams.filter(
                (team) => team.name.includes(search) || team.description.includes(search)
            );
        }

        if (filteredTeams.length === 0) {
            return successResponseCreator(200, "Get teams successfully", []);
        }

        if (limit && offset) {
            if (offset * limit + 1 > filteredTeams.length) {
                throw this.fastify.httpErrors.badRequest("Offset is not exists");
            }
            filteredTeams = filteredTeams.slice(offset * limit, (offset + 1) * limit);
        }

        return successResponseCreator(200, "Get teams successfully", filteredTeams);
    }

    async update_team(teams, name, description, avatarUrl, isPublished) {
        const team = teams.find((team) => team.name === name);
        if (!team) {
            throw this.fastify.httpErrors.badRequest("Team is not exists");
        }

        team.description = description;
        team.avatarUrl = avatarUrl;
        team.isPublished = isPublished;
        const savedTeam = await team.save();
        if (!savedTeam) {
            throw this.fastify.httpErrors.internalServerError("Something went wrong");
        }

        return successResponseCreator(200, "Update a team successfully", team);
    }

    async remove_team(user, teams, name) {
        const teamIndex = teams.findIndex((team) => team.name === name);
        if (teamIndex === -1) {
            throw this.fastify.httpErrors.badRequest("Team is not exists");
        }

        const removedTeam = teams[teamIndex];
        teams.splice(teamIndex, 1);
        const savedUser = await user.save();
        if (!savedUser) {
            throw this.fastify.httpErrors.internalServerError("Something went wrong");
        }

        const result = await Team.deleteOne({name});
        if (!result.ok) {
            throw this.fastify.httpErrors.internalServerError("Something went wrong");
        }

        return successResponseCreator(200, "Delete a team successfully", removedTeam);
    }

    async addUser_team(user, teams, name, id) {
        if (!objectIdReg.test(id)) {
            throw this.fastify.httpErrors.badRequest("UserId is invalid");
        }

        if (user._id.toString() === id) {
            throw this.fastify.httpErrors.badRequest("User is the admin of the team");
        }

        const teamIndex = teams.findIndex((team) => team.name === name);
        if (teamIndex === -1) {
            throw this.fastify.httpErrors.badRequest("Team is not exists");
        }

        const existsUser = await User.findById(id);
        if (!existsUser) {
            throw this.fastify.httpErrors.badRequest("User is not exists");
        }

        const team = teams[teamIndex];
        if (team.users.find((userId) => userId.toString() === id)) {
            throw this.fastify.httpErrors.badRequest("User is already in the team");
        }

        team.users.push(id);
        const savedTeam = await team.save();
        if (!savedTeam) {
            throw this.fastify.httpErrors.internalServerError("Something went wrong");
        }

        existsUser.teams.push(team._id);
        const savedUser = await existsUser.save();
        if (!savedUser) {
            throw this.fastify.httpErrors.internalServerError("Something went wrong");
        }

        return successResponseCreator(200, "Add a user successfully");
    }
}

module.exports = TeamService;
