Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions lib/daos/driverLocationDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const updateDriverLocation = async (
...options,
},
);
return { driverLocation };
return driverLocation;
};

export const getDriverLocationByDriverId = async (driverId, options = {}) => {
Expand All @@ -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: [
[
Expand All @@ -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: [
[
Expand All @@ -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),
};
};
45 changes: 24 additions & 21 deletions lib/daos/paymentDao.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
25 changes: 6 additions & 19 deletions lib/daos/pricingConfigDao.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -30,12 +17,12 @@ export const updatePricingConfig = async (pricingConfigProps, options = {}) => {
};

export const findAllPricingConfigs = async (page, limit) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also return the total number of items i.e. length? How can the frontend know how many pages there will be?

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) => {
Expand Down
40 changes: 25 additions & 15 deletions lib/daos/tests/driverLocationDao.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
57 changes: 33 additions & 24 deletions lib/daos/tests/paymentDao.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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');
});
});

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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);
});
});
});
Loading