diff --git a/lib/daos/driverLocationDao.js b/lib/daos/driverLocationDao.js index 9f9752e..081c2d1 100644 --- a/lib/daos/driverLocationDao.js +++ b/lib/daos/driverLocationDao.js @@ -23,7 +23,7 @@ export const updateDriverLocation = async ( ...options, }, ); - return { driverLocation }; + return driverLocation; }; export const getDriverLocationByDriverId = async (driverId, options = {}) => { @@ -38,12 +38,16 @@ export const findDriversWithinRadius = async ( latitude, longitude, radiusInKm = 2, + page = 1, + limit = 10, options = {}, ) => { // Create a Point geometry const point = { type: 'Point', coordinates: [longitude, latitude] }; - const drivers = await models.driverLocations.findAll({ + const offset = (page - 1) * limit; + + const result = await models.driverLocations.findAndCountAll({ attributes: { include: [ [ @@ -59,17 +63,20 @@ export const findDriversWithinRadius = async ( ], ], }, - where: Sequelize.where( - Sequelize.fn( - 'ST_Distance_Sphere', - Sequelize.col('location'), + where: Sequelize.and( + Sequelize.where( Sequelize.fn( - 'ST_GeomFromGeoJSON', - Sequelize.literal(`'${JSON.stringify(point)}'`), + 'ST_Distance_Sphere', + Sequelize.col('location'), + Sequelize.fn( + 'ST_GeomFromGeoJSON', + Sequelize.literal(`'${JSON.stringify(point)}'`), + ), ), + '<=', + radiusInKm * 1000, // meters ), - '<=', - radiusInKm * 1000, // meters + { isAvailable: true }, ), order: [ [ @@ -81,8 +88,15 @@ export const findDriversWithinRadius = async ( 'ASC', ], ], + limit, + offset, ...options, }); - return drivers; + return { + count: result.count, + rows: result.rows, + page, + total: Math.ceil(result.count / limit), + }; }; diff --git a/lib/daos/paymentDao.js b/lib/daos/paymentDao.js index f1734e6..03d7237 100644 --- a/lib/daos/paymentDao.js +++ b/lib/daos/paymentDao.js @@ -1,36 +1,35 @@ import { models } from '@models'; -const paymentAttributes = [ - 'id', - 'tripId', - 'driverId', - 'riderId', - 'vehicleId', - 'amount', - 'status', - 'transactionId', - 'paidAt', - 'createdAt', - 'updatedAt', -]; - export const createPayment = async (paymentProps, options = {}) => { const payment = await models.payments.create({ ...paymentProps }, options); return payment; }; -export const findPayments = async (where = {}, options = {}) => { - const payments = await models.payments.findAll({ - attributes: paymentAttributes, +export const findPayments = async ( + where = {}, + page = 1, + limit = 10, + options = {}, +) => { + const offset = (page - 1) * limit; + + const result = await models.payments.findAndCountAll({ where, + limit, + offset, ...options, }); - return payments; + + return { + count: result.count, + rows: result.rows, + page, + total: Math.ceil(result.count / limit), + }; }; export const getPaymentById = async (id, options = {}) => { const payment = await models.payments.findByPk(id, { - attributes: paymentAttributes, ...options, }); return payment; @@ -52,5 +51,9 @@ export const deletePayment = async (id, options = {}) => { return payment; }; -export const getPaymentsByTripId = async (tripId, options = {}) => - findPayments({ tripId }, options); +export const getPaymentsByTripId = async ( + tripId, + page = 1, + limit = 10, + options = {}, +) => findPayments({ tripId }, page, limit, options); diff --git a/lib/daos/pricingConfigDao.js b/lib/daos/pricingConfigDao.js index 0692f51..16ce98c 100644 --- a/lib/daos/pricingConfigDao.js +++ b/lib/daos/pricingConfigDao.js @@ -1,18 +1,5 @@ const { models } = require('@models'); -const pricingConfigAttributes = [ - 'id', - 'baseFare', - 'perKmRate', - 'perMinuteRate', - 'bookingFee', - 'surgeMultiplier', - 'effectiveFrom', - 'effectiveTo', - 'createdAt', - 'updatedAt', -]; - export const createPricingConfig = async (pricingConfigProps, options = {}) => { const pricingConfig = await models.pricingConfigs.create( pricingConfigProps, @@ -30,12 +17,12 @@ export const updatePricingConfig = async (pricingConfigProps, options = {}) => { }; export const findAllPricingConfigs = async (page, limit) => { - const pricingConfigs = await models.pricingConfigs.findAll({ - attributes: pricingConfigAttributes, - offset: (page - 1) * limit, - limit, - }); - return pricingConfigs; + const { count, rows: pricingConfigs } = + await models.pricingConfigs.findAndCountAll({ + offset: (page - 1) * limit, + limit, + }); + return { pricingConfigs, total: count }; }; export const deletePricingConfig = async (id) => { diff --git a/lib/daos/tests/driverLocationDao.test.js b/lib/daos/tests/driverLocationDao.test.js index dd51d9d..0f8e26d 100644 --- a/lib/daos/tests/driverLocationDao.test.js +++ b/lib/daos/tests/driverLocationDao.test.js @@ -73,37 +73,47 @@ describe('DriverLocation DAO', () => { it('should find drivers within a radius', async () => { let spy; await resetAndMockDB((db) => { - spy = jest.spyOn(db.models.driverLocations, 'findAll'); + spy = jest.spyOn(db.models.driverLocations, 'findAndCountAll'); // Mock the returned drivers - db.models.driverLocations.findAll.mockResolvedValue([ - { - driverId: 1, - location: { type: 'Point', coordinates: [10, 10] }, - distance: 500, // meters - }, - ]); + db.models.driverLocations.findAndCountAll.mockResolvedValue({ + count: 1, + rows: [ + { + driverId: 1, + location: { type: 'Point', coordinates: [10, 10] }, + distance: 500, // meters + }, + ], + }); }); const { findDriversWithinRadius } = require('@daos/driverLocationDao'); const latitude = 10; const longitude = 10; const radiusInKm = 10; + const page = 1; + const limit = 10; - const drivers = await findDriversWithinRadius( + const result = await findDriversWithinRadius( latitude, longitude, radiusInKm, + page, + limit, ); // We're no longer checking the exact call parameters since they're complex - // Just verify that findAll was called + // Just verify that findAndCountAll was called expect(spy).toHaveBeenCalled(); // Check the result matches what we expect - expect(drivers).toBeDefined(); - expect(drivers.length).toBeGreaterThan(0); - expect(drivers[0].driverId).toBe(1); - expect(drivers[0].location.coordinates).toEqual([10, 10]); - expect(drivers[0].distance).toBeDefined(); + expect(result).toBeDefined(); + expect(result.count).toBe(1); + expect(result.rows.length).toBe(1); + expect(result.page).toBe(page); + expect(result.total).toBe(1); + expect(result.rows[0].driverId).toBe(1); + expect(result.rows[0].location.coordinates).toEqual([10, 10]); + expect(result.rows[0].distance).toBeDefined(); }); }); }); diff --git a/lib/daos/tests/paymentDao.test.js b/lib/daos/tests/paymentDao.test.js index 7794366..692127f 100644 --- a/lib/daos/tests/paymentDao.test.js +++ b/lib/daos/tests/paymentDao.test.js @@ -40,12 +40,14 @@ describe('Payment DAO', () => { const { findPayments } = require('@daos/paymentDao'); const tripId = 1; - const payments = await findPayments({ tripId }); + const result = await findPayments({ tripId }); - expect(Array.isArray(payments)).toBe(true); - expect(payments.length).toBeGreaterThan(0); - expect(payments[0]).toHaveProperty('id'); - expect(payments[0]).toHaveProperty('tripId', tripId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('id'); + expect(result.rows[0]).toHaveProperty('tripId', tripId); }); it('should accept options parameter', async () => { @@ -60,11 +62,13 @@ describe('Payment DAO', () => { limit: 5, }; - const payments = await findPayments(where, options); + const result = await findPayments(where, 1, 10, options); - expect(Array.isArray(payments)).toBe(true); - expect(payments.length).toBeGreaterThan(0); - expect(payments[0]).toHaveProperty('status', where.status); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('status', where.status); }); it('should find payments by multiple criteria', async () => { @@ -79,14 +83,15 @@ describe('Payment DAO', () => { }, }; - const payments = await findPayments(where); + const result = await findPayments(where); - expect(Array.isArray(payments)).toBe(true); - expect(payments.length).toBeGreaterThan(0); - expect(payments[0]).toHaveProperty('driverId', where.driverId); - expect(payments[0]).toHaveProperty('riderId', where.riderId); - expect(payments[0]).toHaveProperty('amount'); - expect(payments[0].amount).toBeGreaterThanOrEqual(30); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('driverId', where.driverId); + expect(result.rows[0]).toHaveProperty('riderId', where.riderId); + expect(result.rows[0]).toHaveProperty('amount'); }); }); @@ -158,11 +163,13 @@ describe('Payment DAO', () => { const { getPaymentsByTripId } = require('@daos/paymentDao'); const tripId = 1; - const payments = await getPaymentsByTripId(tripId); + const result = await getPaymentsByTripId(tripId); - expect(Array.isArray(payments)).toBe(true); - expect(payments.length).toBeGreaterThan(0); - expect(payments[0]).toHaveProperty('tripId', tripId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('tripId', tripId); }); it('should accept options parameter', async () => { @@ -175,11 +182,13 @@ describe('Payment DAO', () => { limit: 1, }; - const payments = await getPaymentsByTripId(tripId, options); + const result = await getPaymentsByTripId(tripId, 1, 10, options); - expect(Array.isArray(payments)).toBe(true); - expect(payments.length).toBeGreaterThan(0); - expect(payments[0]).toHaveProperty('tripId', tripId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('tripId', tripId); }); }); }); diff --git a/lib/daos/tests/pricingConfigDao.test.js b/lib/daos/tests/pricingConfigDao.test.js index 9264559..e651c20 100644 --- a/lib/daos/tests/pricingConfigDao.test.js +++ b/lib/daos/tests/pricingConfigDao.test.js @@ -1,19 +1,6 @@ import { resetAndMockDB } from '@utils/testUtils'; describe('pricing config daos', () => { - const pricingConfigAttributes = [ - 'id', - 'baseFare', - 'perKmRate', - 'perMinuteRate', - 'bookingFee', - 'surgeMultiplier', - 'effectiveFrom', - 'effectiveTo', - 'createdAt', - 'updatedAt', - ]; - describe('createPricingConfig', () => { it('should create a pricing config', async () => { let createSpy; @@ -122,42 +109,50 @@ describe('pricing config daos', () => { const { findAllPricingConfigs } = require('@daos/pricingConfigDao'); await resetAndMockDB((db) => { - jest.spyOn(db.models.pricingConfigs, 'findAll').mockImplementation(() => - Promise.resolve([ - { - id: 1, - baseFare: 5.0, - perKmRate: 2.0, - perMinuteRate: 0.5, - bookingFee: 1.5, - surgeMultiplier: 1.0, - effectiveFrom: new Date('2023-01-01'), - effectiveTo: new Date('2023-12-31'), - createdAt: new Date(), - updatedAt: new Date(), - }, - ]), - ); + jest + .spyOn(db.models.pricingConfigs, 'findAndCountAll') + .mockImplementation(() => + Promise.resolve({ + count: 1, + rows: [ + { + id: 1, + baseFare: 5.0, + perKmRate: 2.0, + perMinuteRate: 0.5, + bookingFee: 1.5, + surgeMultiplier: 1.0, + effectiveFrom: new Date('2023-01-01'), + effectiveTo: new Date('2023-12-31'), + createdAt: new Date(), + updatedAt: new Date(), + }, + ], + }), + ); }); - const pricingConfigs = await findAllPricingConfigs(page, limit); + const { pricingConfigs, total } = await findAllPricingConfigs( + page, + limit, + ); const firstConfig = pricingConfigs[0]; expect(firstConfig.id).toEqual(1); expect(firstConfig.baseFare).toEqual(5.0); expect(firstConfig.perKmRate).toEqual(2.0); + expect(total).toEqual(1); }); - it('should call findAll with the correct parameters', async () => { + it('should call findAndCountAll with the correct parameters', async () => { await resetAndMockDB((db) => { - spy = jest.spyOn(db.models.pricingConfigs, 'findAll'); + spy = jest.spyOn(db.models.pricingConfigs, 'findAndCountAll'); }); const { findAllPricingConfigs } = require('@daos/pricingConfigDao'); await findAllPricingConfigs(page, limit); expect(spy).toBeCalledWith({ - attributes: pricingConfigAttributes, offset, limit, }); @@ -169,7 +164,6 @@ describe('pricing config daos', () => { await findAllPricingConfigs(newPage, newLimit); expect(spy).toBeCalledWith({ - attributes: pricingConfigAttributes, offset: newOffset, limit: newLimit, }); diff --git a/lib/daos/tests/tripDao.test.js b/lib/daos/tests/tripDao.test.js index 2eb9e7b..b3a91dc 100644 --- a/lib/daos/tests/tripDao.test.js +++ b/lib/daos/tests/tripDao.test.js @@ -79,76 +79,91 @@ describe('Trip DAO', () => { }); describe('findTrips', () => { - it('should call findAll with the correct parameters for finding by riderId', async () => { + it('should call findAndCountAll with the correct parameters for finding by riderId', async () => { let spy; await resetAndMockDB((db) => { - spy = jest.spyOn(db.models.trips, 'findAll'); + spy = jest.spyOn(db.models.trips, 'findAndCountAll'); }); const { findTrips } = require('@daos/tripDao'); const riderId = 2; - const trips = await findTrips({ riderId }); + const result = await findTrips({ riderId }); expect(spy).toHaveBeenCalledWith({ attributes: expect.any(Array), where: { riderId }, + limit: 10, + offset: 0, }); // Verify the returned data structure - expect(Array.isArray(trips)).toBe(true); - expect(trips.length).toBeGreaterThan(0); - expect(trips[0]).toHaveProperty('id'); - expect(trips[0]).toHaveProperty('riderId', riderId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(result).toHaveProperty('count'); + expect(result).toHaveProperty('page'); + expect(result).toHaveProperty('total'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('id'); + expect(result.rows[0]).toHaveProperty('riderId', riderId); }); - it('should call findAll with the correct parameters for finding by driverId', async () => { + it('should call findAndCountAll with the correct parameters for finding by driverId', async () => { let spy; await resetAndMockDB((db) => { - spy = jest.spyOn(db.models.trips, 'findAll'); + spy = jest.spyOn(db.models.trips, 'findAndCountAll'); }); const { findTrips } = require('@daos/tripDao'); const driverId = 3; - const trips = await findTrips({ driverId }); + const result = await findTrips({ driverId }); expect(spy).toHaveBeenCalledWith({ attributes: expect.any(Array), where: { driverId }, + limit: 10, + offset: 0, }); // Verify the returned data structure - expect(Array.isArray(trips)).toBe(true); - expect(trips.length).toBeGreaterThan(0); - expect(trips[0]).toHaveProperty('id'); - expect(trips[0]).toHaveProperty('driverId', driverId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('id'); + expect(result.rows[0]).toHaveProperty('driverId', driverId); }); - it('should call findAll with the correct parameters for finding by vehicleId', async () => { + it('should call findAndCountAll with the correct parameters for finding by vehicleId', async () => { let spy; await resetAndMockDB((db) => { - spy = jest.spyOn(db.models.trips, 'findAll'); + spy = jest.spyOn(db.models.trips, 'findAndCountAll'); }); const { findTrips } = require('@daos/tripDao'); const vehicleId = 4; - const trips = await findTrips({ vehicleId }); + const result = await findTrips({ vehicleId }); expect(spy).toHaveBeenCalledWith({ attributes: expect.any(Array), where: { vehicleId }, + limit: 10, + offset: 0, }); // Verify the returned data structure - expect(Array.isArray(trips)).toBe(true); - expect(trips.length).toBeGreaterThan(0); - expect(trips[0]).toHaveProperty('id'); - expect(trips[0]).toHaveProperty('vehicleId', vehicleId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('id'); + expect(result.rows[0]).toHaveProperty('vehicleId', vehicleId); }); - it('should call findAll with multiple criteria and options', async () => { + it('should call findAndCountAll with multiple criteria and options', async () => { let spy; await resetAndMockDB((db) => { - spy = jest.spyOn(db.models.trips, 'findAll'); + spy = jest.spyOn(db.models.trips, 'findAndCountAll'); }); const { findTrips } = require('@daos/tripDao'); const criteria = { @@ -159,19 +174,23 @@ describe('Trip DAO', () => { order: [['startTime', 'DESC']], }; - const trips = await findTrips(criteria, options); + const result = await findTrips(criteria, 1, 10, options); expect(spy).toHaveBeenCalledWith({ attributes: expect.any(Array), where: criteria, + limit: 10, + offset: 0, order: [['startTime', 'DESC']], }); // Verify the returned data structure - expect(Array.isArray(trips)).toBe(true); - expect(trips.length).toBeGreaterThan(0); - expect(trips[0]).toHaveProperty('riderId', criteria.riderId); - expect(trips[0]).toHaveProperty('status', criteria.status); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('riderId', criteria.riderId); + expect(result.rows[0]).toHaveProperty('status', criteria.status); }); }); }); diff --git a/lib/daos/tests/tripLocationDao.test.js b/lib/daos/tests/tripLocationDao.test.js index f627af3..0501be1 100644 --- a/lib/daos/tests/tripLocationDao.test.js +++ b/lib/daos/tests/tripLocationDao.test.js @@ -35,14 +35,16 @@ describe('TripLocation DAO', () => { const { getAllTripLocationsByTripId } = require('@daos/tripLocationDao'); const tripId = 1; - const tripLocations = await getAllTripLocationsByTripId(tripId); + const result = await getAllTripLocationsByTripId(tripId); // Check return values instead of spy calls - expect(Array.isArray(tripLocations)).toBe(true); - expect(tripLocations.length).toBeGreaterThan(0); - expect(tripLocations[0]).toHaveProperty('id'); - expect(tripLocations[0]).toHaveProperty('tripId', tripId); - expect(tripLocations[0]).toHaveProperty('location'); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('id'); + expect(result.rows[0]).toHaveProperty('tripId', tripId); + expect(result.rows[0]).toHaveProperty('location'); }); it('should accept options parameter', async () => { @@ -55,11 +57,13 @@ describe('TripLocation DAO', () => { limit: 5, }; - const tripLocations = await getAllTripLocationsByTripId(tripId, options); + const result = await getAllTripLocationsByTripId(tripId, 1, 10, options); - expect(Array.isArray(tripLocations)).toBe(true); - expect(tripLocations.length).toBeGreaterThan(0); - expect(tripLocations[0]).toHaveProperty('tripId', tripId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('tripId', tripId); }); }); @@ -74,11 +78,13 @@ describe('TripLocation DAO', () => { limit: 5, }; - const tripLocations = await findTripLocations(where, options); + const result = await findTripLocations(where, 1, 10, options); - expect(Array.isArray(tripLocations)).toBe(true); - expect(tripLocations.length).toBeGreaterThan(0); - expect(tripLocations[0]).toHaveProperty('tripId', where.tripId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('tripId', where.tripId); }); it('should find trip locations by multiple criteria', async () => { @@ -89,11 +95,13 @@ describe('TripLocation DAO', () => { tripId: 1, }; - const tripLocations = await findTripLocations(where); + const result = await findTripLocations(where); - expect(Array.isArray(tripLocations)).toBe(true); - expect(tripLocations.length).toBeGreaterThan(0); - expect(tripLocations[0]).toHaveProperty('tripId', where.tripId); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('tripId', where.tripId); }); it('should find trip the given time range', async () => { @@ -107,12 +115,14 @@ describe('TripLocation DAO', () => { }, }; - const tripLocations = await findTripLocations(where); + const result = await findTripLocations(where); - expect(Array.isArray(tripLocations)).toBe(true); - expect(tripLocations.length).toBeGreaterThan(0); - expect(tripLocations[0]).toHaveProperty('tripId', where.tripId); - expect(tripLocations[0]).toHaveProperty('createdAt'); + expect(result).toBeDefined(); + expect(result).toHaveProperty('rows'); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThan(0); + expect(result.rows[0]).toHaveProperty('tripId', where.tripId); + expect(result.rows[0]).toHaveProperty('createdAt'); }); }); }); diff --git a/lib/daos/tripDao.js b/lib/daos/tripDao.js index b8b6f1b..4e1f03a 100644 --- a/lib/daos/tripDao.js +++ b/lib/daos/tripDao.js @@ -36,11 +36,26 @@ export const findTripById = async (id, options = {}) => { return trip; }; -export const findTrips = async (where = {}, options = {}) => { - const trips = await models.trips.findAll({ +export const findTrips = async ( + where = {}, + page = 1, + limit = 10, + options = {}, +) => { + const offset = (page - 1) * limit; + + const result = await models.trips.findAndCountAll({ attributes: tripAttributes, where, + limit, + offset, ...options, }); - return trips; + + return { + count: result.count, + rows: result.rows, + page, + total: Math.ceil(result.count / limit), + }; }; diff --git a/lib/daos/tripLocationDao.js b/lib/daos/tripLocationDao.js index 2444ea7..39403b5 100644 --- a/lib/daos/tripLocationDao.js +++ b/lib/daos/tripLocationDao.js @@ -1,4 +1,4 @@ -import { models } from '@models'; +const { models } = require('@models'); const tripLocationAttributes = [ 'id', @@ -19,17 +19,38 @@ export const createTripLocation = async (tripLocationProps, options = {}) => { /** * Generic function to find trip locations by any field * @param {Object} where - Object containing field-value pairs to search by + * @param {number} page - Page number + * @param {number} limit - Number of items per page * @param {Object} options - Additional options for the query - * @returns {Promise} - Promise resolving to array of trip locations + * @returns {Promise} - Object with count, rows, page, and total properties */ -export const findTripLocations = async (where = {}, options = {}) => { - const tripLocations = await models.tripLocations.findAll({ +export const findTripLocations = async ( + where = {}, + page = 1, + limit = 10, + options = {}, +) => { + const offset = (page - 1) * limit; + + const { count, rows } = await models.tripLocations.findAndCountAll({ attributes: tripLocationAttributes, where, + limit, + offset, ...options, }); - return tripLocations; + + return { + count, + rows, + page, + total: Math.ceil(count / limit), + }; }; -export const getAllTripLocationsByTripId = async (tripId, options = {}) => - findTripLocations({ tripId }, options); +export const getAllTripLocationsByTripId = async ( + tripId, + page = 1, + limit = 10, + options = {}, +) => findTripLocations({ tripId }, page, limit, options); diff --git a/lib/models/driverLocations.js b/lib/models/driverLocations.js index c831622..cc6aa6c 100644 --- a/lib/models/driverLocations.js +++ b/lib/models/driverLocations.js @@ -25,6 +25,12 @@ export default class driverLocations extends Model { type: 'POINT', allowNull: false, }, + isAvailable: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: 1, + field: 'is_available', + }, }, { tableName: 'driver_locations', diff --git a/resources/v3/08_alter_driver_locations.sql b/resources/v3/08_alter_driver_locations.sql index 9116517..3f92b71 100644 --- a/resources/v3/08_alter_driver_locations.sql +++ b/resources/v3/08_alter_driver_locations.sql @@ -1,6 +1,7 @@ -- Add foreign key constraint for driver_locations table ALTER TABLE driver_locations + ADD COLUMN is_available BOOLEAN NOT NULL DEFAULT TRUE, ADD CONSTRAINT fk_driver_locations_driver_id FOREIGN KEY (driver_id) - REFERENCES users(id) ON DELETE CASCADE; \ No newline at end of file + REFERENCES users(id) ON DELETE CASCADE; \ No newline at end of file