const areaRepository = require('../../repository/sales/AreaRepository.js');
const { SearchFilter } = require('../../utils/SearchFilter.js');
const prisma = require('../../prisma/PrismaClient.js');

exports.getAllAreaByUserService = async ({ page, limit, search, sortBy, orderBy }, req) => {
    const skip = (page - 1) * limit;

    const where = {
        user_id: req.user.id,
        deletedAt: null,
        ...SearchFilter(search, ['province.name']),
    };

    const [areas, total] = await Promise.all([
        areaRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
        areaRepository.countAll(where),
    ]);

    return { areas, total };
};