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
82 changes: 82 additions & 0 deletions lib/daos/driverLocationDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { models } from '@models';
import { Sequelize } from 'sequelize';

export const createDriverLocation = async (
driverLocationProps,
options = {},
) => {
const driverLocation = await models.driverLocations.create(
{ ...driverLocationProps },
options,
);
return driverLocation;
};

export const updateDriverLocation = async (
driverLocationProps,
options = {},
) => {
const driverLocation = await models.driverLocations.update(
driverLocationProps,
{
where: { id: driverLocationProps.id },
...options,
},
);
return { driverLocation };
};

export const getDriverLocationByDriverId = async (driverId, options = {}) => {
const driverLocation = await models.driverLocations.findOne({
where: { driverId },
...options,
});
return driverLocation;
};

export const findDriversWithinRadius = async (
latitude,
longitude,
radiusInKm = 2,
options = {},
) => {
// Create the point as a Well-Known Text (WKT) format
const pointWkt = `POINT(${longitude} ${latitude})`;

const drivers = await models.driverLocations.findAll({
attributes: {
include: [
[
Sequelize.fn(
'ST_Distance_Sphere',
Sequelize.col('location'),
Sequelize.fn('ST_GeomFromText', pointWkt),
),
'distance',
],
],
},
where: Sequelize.where(
Sequelize.fn(
'ST_Distance_Sphere',
Sequelize.col('location'),
Sequelize.fn('ST_GeomFromText', pointWkt),
),
'<=',
radiusInKm * 1000, // meters
),
order: [
[
Sequelize.fn(
'ST_Distance_Sphere',
Sequelize.col('location'),
Sequelize.fn('ST_GeomFromText', pointWkt),
),
'ASC',
],
],
...options,
});

return drivers;
};
101 changes: 70 additions & 31 deletions lib/daos/oauthAccessTokensDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,41 @@ import { convertDbResponseToRawResponse } from '@utils/transformerUtils';
const ttl = server.app.options.oauth.access_token_ttl;
const BEARER = 'Bearer';

export const createAccessToken = async (oauthClientId, timeToLive = ttl) => {
const metadata = await getMetaDataByOAuthClientId(oauthClientId).catch(
(error) => error,
);
export const createAccessToken = async (
oauthClientId,
timeToLive = ttl,
userId = null,
) => {
try {
const metadata = await getMetaDataByOAuthClientId(oauthClientId).catch(
(error) => {
console.error('Error getting OAuth client metadata:', error);
return { scope: {}, resources: [] };
},
);

if (isEmpty(metadata)) {
throw unauthorized(ACCESS_DENIED);
}
if (isEmpty(metadata)) {
throw unauthorized(ACCESS_DENIED);
}

return models.oauthAccessTokens
.create({
accessToken: strippedUUID(),
oauthClientId,
expiresIn: timeToLive,
expiresOn: moment().add(timeToLive, 'seconds').format(TIMESTAMP),
tokenType: BEARER,
metadata: JSON.stringify(metadata),
createdAt: moment(),
})
.then((accessToken) => convertDbResponseToRawResponse(accessToken));
// Add userId to metadata if provided
const metadataWithUserId = userId ? { ...metadata, userId } : metadata;

return models.oauthAccessTokens
.create({
accessToken: strippedUUID(),
oauthClientId,
expiresIn: timeToLive,
expiresOn: moment().add(timeToLive, 'seconds').format(TIMESTAMP),
tokenType: BEARER,
metadata: JSON.stringify(metadataWithUserId),
createdAt: moment(),
})
.then((accessToken) => convertDbResponseToRawResponse(accessToken));
} catch (error) {
console.error('Error creating access token:', error);
throw error;
}
};

/**
Expand All @@ -40,7 +55,8 @@ export const createAccessToken = async (oauthClientId, timeToLive = ttl) => {
* @param {any} accessToken
* @returns {any}
*/
export const findAccessToken = (accessToken) => models.oauthAccessTokens
export const findAccessToken = (accessToken) =>
models.oauthAccessTokens
.findOne({
attributes: [
'accessToken',
Expand All @@ -58,12 +74,15 @@ export const findAccessToken = (accessToken) => models.oauthAccessTokens
include: [
{
model: models.oauthClients,
as: 'oauthClient',
include: [
{
model: models.oauthClientResources,
as: 'oauthClientResources',
},
{
model: models.oauthClientScopes,
as: 'oauthClientScope',
},
],
},
Expand All @@ -72,21 +91,41 @@ export const findAccessToken = (accessToken) => models.oauthAccessTokens
})
.then((token) => {
if (token) {
return convertDbResponseToRawResponse(token);
const convertedToken = convertDbResponseToRawResponse(token);
// Parse metadata JSON if it exists and is a string
// if (
// convertedToken.metadata &&
// typeof convertedToken.metadata === 'string'
// ) {
// try {
// const parsedMetadata = JSON.parse(convertedToken.metadata);
// convertedToken.metadata = parsedMetadata;

// // Also add userId directly to credentials for easy access
// if (parsedMetadata.userId) {
// convertedToken.userId = parsedMetadata.userId;
// }
// } catch (error) {
// console.error('Error parsing token metadata:', error);
// }
// }

return convertedToken;
}
return token;
});

export const updateAccessToken = (accessToken, timeToLive) => models.oauthAccessTokens.update(
{
accessToken,
expiresIn: ttl,
expiresOn: moment().add(timeToLive, 'seconds').format(TIMESTAMP),
},
{
where: {
export const updateAccessToken = (accessToken, timeToLive) =>
models.oauthAccessTokens.update(
{
accessToken,
expiresIn: ttl,
expiresOn: moment().add(timeToLive, 'seconds').format(TIMESTAMP),
},
{
where: {
accessToken,
},
underscoredAll: false,
},
underscoredAll: false,
},
);
);
21 changes: 12 additions & 9 deletions lib/daos/oauthClientsDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ export const getMetaDataByOAuthClientId = (id) =>
models.oauthClients
.findOne({
where: { id },
include: [models.oauthClientResources, models.oauthClientScopes],
include: [
{ model: models.oauthClientResources, as: 'oauthClientResources' },
{ model: models.oauthClientScopes, as: 'oauthClientScope' },
],
})
.then((oauthClient) => {
if (!oauthClient) {
return null;
}
const resources = transformDbArrayResponseToRawResponse(
oauthClient.oauth_client_resources
oauthClient.oauthClientResources,
);

const scope = convertDbResponseToRawResponse(
oauthClient.oauth_client_scope
oauthClient.oauthClientScope,
);

const metadata = {
Expand All @@ -56,7 +59,7 @@ export const getMetaDataByOAuthClientId = (id) =>

export const createOauthClient = async (
{ clientId, clientSecret = '', grantType, scope, resources },
t
t,
) => {
let secret = '';
if (grantType === GRANT_TYPE.CLIENT_CREDENTIALS) {
Expand All @@ -73,22 +76,22 @@ export const createOauthClient = async (
clientSecret: secret,
grantType,
},
{ transaction: t }
)
{ transaction: t },
),
);
let oauthClientResourcesObj = null;
let oauthClientScope = null;
if (resources) {
oauthClientResourcesObj = transformDbArrayResponseToRawResponse(
await createOauthResources(
{ oauthClientId: oauthClient.id, resources },
t
)
t,
),
);
}
if (scope) {
oauthClientScope = convertDbResponseToRawResponse(
await createOauthScope({ oauthClientId: oauthClient.id, scope }, t)
await createOauthScope({ oauthClientId: oauthClient.id, scope }, t),
);
}
return {
Expand Down
56 changes: 56 additions & 0 deletions lib/daos/paymentDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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,
where,
...options,
});
return payments;
};

export const getPaymentById = async (id, options = {}) => {
const payment = await models.payments.findByPk(id, {
attributes: paymentAttributes,
...options,
});
return payment;
};

export const updatePayment = async (id, paymentProps, options = {}) => {
const payment = await models.payments.update(
{ ...paymentProps },
{ where: { id }, ...options },
);
return payment;
};

export const deletePayment = async (id, options = {}) => {
const payment = await models.payments.destroy({
where: { id },
...options,
});
return payment;
};

export const getPaymentsByTripId = async (tripId, options = {}) =>
findPayments({ tripId }, options);
46 changes: 46 additions & 0 deletions lib/daos/pricingConfigDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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,
options,
);
return pricingConfig;
};

export const updatePricingConfig = async (pricingConfigProps, options = {}) => {
const pricingConfig = await models.pricingConfigs.update(pricingConfigProps, {
where: { id: pricingConfigProps.id },
...options,
});
return pricingConfig;
};

export const findAllPricingConfigs = async (page, limit) => {
const pricingConfigs = await models.pricingConfigs.findAll({
attributes: pricingConfigAttributes,
offset: (page - 1) * limit,
limit,
});
return pricingConfigs;
};

export const deletePricingConfig = async (id) => {
const pricingConfig = await models.pricingConfigs.destroy({
where: { id },
});
return pricingConfig;
};
Loading