Use of utils array check + use array of IDs to get a single record
MODIFY
src/storage/mongodb/AssetStorage.ts
src/storage/mongodb/AssetStorage.ts
@@ -133,15 +133,19 @@ export default class AssetStorage {
// With no Site Area
if (params.withNoSiteArea) {
filters.siteAreaID = null;
- } else if (params.siteAreaIDs && Array.isArray(params.siteAreaIDs) && params.siteAreaIDs.length > 0) {
- filters.siteAreaID = { $in: params.siteAreaIDs.map((id) => Utils.convertToObjectID(id)) };
+ } else if (!Utils.isEmptyArray(params.siteAreaIDs)) {
+ filters.siteAreaID = {
+ $in: params.siteAreaIDs.map((id) => Utils.convertToObjectID(id))
+ };
}
// Create Aggregation
const aggregation = [];
// Limit on Asset for Basic Users
- if (params.assetIDs && params.assetIDs.length > 0) {
+ if (!Utils.isEmptyArray(params.assetIDs)) {
// Build filter
- filters._id = { $in: params.assetIDs.map((assetID) => Utils.convertToObjectID(assetID)) };
+ filters._id = {
+ $in: params.assetIDs.map((assetID) => Utils.convertToObjectID(assetID))
+ };
}
// Filters
if (!Utils.isEmptyJSon(filters)) {
@@ -234,7 +238,7 @@ export default class AssetStorage {
{ 'name': { $regex: searchRegex, $options: 'i' } },
];
}
- if (params.siteAreaIDs && Array.isArray(params.siteAreaIDs) && params.siteAreaIDs.length > 0) {
+ if (!Utils.isEmptyArray(params.siteAreaIDs)) {
filters.siteAreaID = { $in: params.siteAreaIDs.map((id) => Utils.convertToObjectID(id)) };
}
// Create Aggregation
@@ -247,7 +251,7 @@ export default class AssetStorage {
}
// Build facets for each type of error if any
const facets: any = { $facet: {} };
- if (params.errorType && Array.isArray(params.errorType) && params.errorType.length > 0) {
+ if (!Utils.isEmptyArray(params.errorType)) {
// Build facet only for one error type
const array = [];
params.errorType.forEach((type) => {

MODIFY
src/storage/mongodb/BillingStorage.ts
src/storage/mongodb/BillingStorage.ts
@@ -60,18 +60,24 @@ export default class BillingStorage {
}
// Create Aggregation
const aggregation = [];
- if (params.invoiceIDs) {
- filters._id = { $in: params.invoiceIDs.map((invoiceID) => Utils.convertToObjectID(invoiceID)) };
+ if (!Utils.isEmptyArray(params.invoiceIDs)) {
+ filters._id = {
+ $in: params.invoiceIDs.map((invoiceID) => Utils.convertToObjectID(invoiceID))
+ };
}
- if (params.userIDs) {
- filters.userID = { $in: params.userIDs.map((userID) => Utils.convertToObjectID(userID)) };
+ if (!Utils.isEmptyArray(params.userIDs)) {
+ filters.userID = {
+ $in: params.userIDs.map((userID) => Utils.convertToObjectID(userID))
+ };
}
if (params.billingInvoiceID) {
filters.invoiceID = { $eq: params.billingInvoiceID };
}
// Status
- if (params.invoiceStatus && Array.isArray(params.invoiceStatus) && params.invoiceStatus.length > 0) {
- filters.status = { $in: params.invoiceStatus };
+ if (!Utils.isEmptyArray(params.invoiceStatus)) {
+ filters.status = {
+ $in: params.invoiceStatus
+ };
}
if (params.startDateTime || params.endDateTime) {
filters.createdOn = {};

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -48,7 +48,6 @@ export default class CarStorage {
const aggregation = [];
// Limit on Car for Basic Users
if (!Utils.isEmptyArray(params.carCatalogIDs)) {
- // Build filter
aggregation.push({
$match: {
_id: { $in: params.carCatalogIDs.map((carCatalogID) => Utils.convertToInt(carCatalogID)) }
@@ -587,9 +586,11 @@ export default class CarStorage {
if (params.vin) {
filters.vin = params.vin;
}
- // Limit on Car for Basic Users
+ // Car
if (!Utils.isEmptyArray(params.carIDs)) {
- filters._id = { $in: params.carIDs.map((carID) => Utils.convertToObjectID(carID)) };
+ filters._id = {
+ $in: params.carIDs.map((carID) => Utils.convertToObjectID(carID))
+ };
}
// Create Aggregation
const aggregation = [];

MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -103,7 +103,7 @@ export default class ChargingStationStorage {
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getChargingStation');
// Query single Charging Station
const chargingStationsMDB = await ChargingStationStorage.getChargingStations(tenantID,
- { chargingStationID: id, withSite: true, ...params }, Constants.DB_PARAMS_SINGLE_RECORD);
+ { chargingStationIDs: [id], withSite: true, ...params }, Constants.DB_PARAMS_SINGLE_RECORD);
// Debug
Logging.traceEnd(MODULE_NAME, 'getChargingStation', uniqueTimerID, { id });
return chargingStationsMDB.result[0];
@@ -111,7 +111,7 @@ export default class ChargingStationStorage {
public static async getChargingStations(tenantID: string,
params: {
- search?: string; chargingStationID?: string; siteAreaIDs?: string[]; withNoSiteArea?: boolean;
+ search?: string; chargingStationIDs?: string[]; siteAreaIDs?: string[]; withNoSiteArea?: boolean;
connectorStatuses?: string[]; connectorTypes?: string[]; statusChangedBefore?: Date;
siteIDs?: string[]; withSite?: boolean; includeDeleted?: boolean; offlineSince?: Date; issuer?: boolean;
},
@@ -129,16 +129,19 @@ export default class ChargingStationStorage {
// Set the filters
const filters: any = { $or: DatabaseUtils.getNotDeletedFilter() };
// Filter
- if (params.chargingStationID) {
- filters._id = params.chargingStationID;
- // Search filters
- } else if (params.search) {
+ if (params.search) {
filters.$or = [
{ '_id': { $regex: params.search, $options: 'i' } },
{ 'chargePointModel': { $regex: params.search, $options: 'i' } },
{ 'chargePointVendor': { $regex: params.search, $options: 'i' } }
];
}
+ // Charging Stations
+ if (!Utils.isEmptyArray(params.chargingStationIDs)) {
+ filters._id = {
+ $in: params.chargingStationIDs
+ };
+ }
// Filter on last heart beat
if (params.offlineSince && moment(params.offlineSince).isValid()) {
filters.lastHeartBeat = { $lte: params.offlineSince };
@@ -201,7 +204,7 @@ export default class ChargingStationStorage {
filters.siteAreaID = null;
} else {
// Query by siteAreaID
- if (params.siteAreaIDs && Array.isArray(params.siteAreaIDs)) {
+ if (!Utils.isEmptyArray(params.siteAreaIDs)) {
filters.siteAreaID = { $in: params.siteAreaIDs.map((id) => Utils.convertToObjectID(id)) };
}
// Site Area
@@ -210,7 +213,7 @@ export default class ChargingStationStorage {
asField: 'siteArea', oneToOneCardinality: true, objectIDFields: ['createdBy', 'lastChangedBy']
});
// Check Site ID
- if (params.siteIDs && Array.isArray(params.siteIDs)) {
+ if (!Utils.isEmptyArray(params.siteIDs)) {
// Build filter
aggregation.push({
$match: {
@@ -315,7 +318,7 @@ export default class ChargingStationStorage {
// Set the filters
const filters: any = { '$or': DatabaseUtils.getNotDeletedFilter() };
filters.issuer = true;
- if (params.siteAreaIDs && Array.isArray(params.siteAreaIDs) && params.siteAreaIDs.length > 0) {
+ if (!Utils.isEmptyArray(params.siteAreaIDs)) {
filters.siteAreaID = { $in: params.siteAreaIDs.map((id) => Utils.convertToObjectID(id)) };
}
// Search filters
@@ -344,7 +347,7 @@ export default class ChargingStationStorage {
$unwind: { 'path': '$sitearea', 'preserveNullAndEmptyArrays': true }
});
// Check Site ID
- if (params.siteIDs && Array.isArray(params.siteIDs) && params.siteIDs.length > 0) {
+ if (!Utils.isEmptyArray(params.siteIDs)) {
aggregation.push({
$match: {
'sitearea.siteID': {
@@ -355,7 +358,7 @@ export default class ChargingStationStorage {
}
// Build facets for each type of error if any
const facets: any = { $facet: {} };
- if (params.errorType && Array.isArray(params.errorType) && params.errorType.length > 0) {
+ if (!Utils.isEmptyArray(params.errorType)) {
// Check allowed
if (!Utils.isTenantComponentActive(await TenantStorage.getTenant(tenantID), TenantComponents.ORGANIZATION) && params.errorType.includes(ChargingStationInErrorType.MISSING_SITE_AREA)) {
throw new BackendError({

MODIFY
src/storage/mongodb/CompanyStorage.ts
src/storage/mongodb/CompanyStorage.ts
@@ -87,7 +87,7 @@ export default class CompanyStorage {
// Check Skip
const skip = Utils.checkRecordSkip(dbParams.skip);
// Set the filters
- const filters: ({ _id?: ObjectID; $or?: any[] } | undefined) = {};
+ const filters: any = {};
if (params.search) {
const searchRegex = Utils.escapeSpecialCharsInRegex(params.search);
filters.$or = [
@@ -99,13 +99,11 @@ export default class CompanyStorage {
// Create Aggregation
const aggregation = [];
// Limit on Company for Basic Users
- if (params.companyIDs && params.companyIDs.length > 0) {
+ if (!Utils.isEmptyArray(params.companyIDs)) {
// Build filter
- aggregation.push({
- $match: {
- _id: { $in: params.companyIDs.map((companyID) => Utils.convertToObjectID(companyID)) }
- }
- });
+ filters._id = {
+ $in: params.companyIDs.map((companyID) => Utils.convertToObjectID(companyID))
+ };
}
if (params.issuer === true || params.issuer === false) {
aggregation.push({

MODIFY
src/storage/mongodb/ConnectionStorage.ts
src/storage/mongodb/ConnectionStorage.ts
@@ -128,5 +128,4 @@ export default class ConnectionStorage {
// Debug
Logging.traceEnd(MODULE_NAME, 'deleteConnectionByUser', uniqueTimerID, { userId });
}
-
}

MODIFY
src/storage/mongodb/RegistrationTokenStorage.ts
src/storage/mongodb/RegistrationTokenStorage.ts
@@ -60,7 +60,7 @@ export default class RegistrationTokenStorage {
filters._id = Utils.convertToObjectID(params.id);
}
- if (params.siteIDs && Array.isArray(params.siteIDs) && params.siteIDs.length > 0) {
+ if (!Utils.isEmptyArray(params.siteIDs)) {
// Build filter
filters['siteArea.siteID'] = {
$in: params.siteIDs

MODIFY
src/storage/mongodb/SiteAreaStorage.ts
src/storage/mongodb/SiteAreaStorage.ts
@@ -89,7 +89,7 @@ export default class SiteAreaStorage {
const siteAreaResult = await SiteAreaStorage.getSiteAreas(
tenantID,
{
- siteAreaID: id,
+ siteAreaIDs: [id],
withSite: params.withSite,
withChargingStations: params.withChargingStations,
withAvailableChargingStations: true
@@ -143,7 +143,7 @@ export default class SiteAreaStorage {
public static async getSiteAreas(tenantID: string,
params: {
- siteAreaID?: string; search?: string; siteIDs?: string[]; withSite?: boolean; issuer?: boolean;
+ siteAreaIDs?: string[]; search?: string; siteIDs?: string[]; withSite?: boolean; issuer?: boolean;
withChargingStations?: boolean; withOnlyChargingStations?: boolean; withAvailableChargingStations?: boolean; smartCharging?: boolean;
} = {},
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<SiteArea>> {
@@ -157,19 +157,22 @@ export default class SiteAreaStorage {
const skip = Utils.checkRecordSkip(dbParams.skip);
// Set the filters
const filters: any = {};
- // Query by Site Area ID if available
- if (params.siteAreaID) {
- filters._id = Utils.convertToObjectID(params.siteAreaID);
// Otherwise check if search is present
- } else if (params.search) {
+ if (params.search) {
filters.$or = [
{ 'name': { $regex: Utils.escapeSpecialCharsInRegex(params.search), $options: 'i' } }
];
}
- // Set Site thru a filter in the dashboard
- if (params.siteIDs && !Utils.isEmptyArray(params.siteIDs)) {
+ // Site Area
+ if (!Utils.isEmptyArray(params.siteAreaIDs)) {
+ filters._id = {
+ $in: params.siteAreaIDs.map((siteAreaID) => Utils.convertToObjectID(siteAreaID))
+ };
+ }
+ // Site
+ if (!Utils.isEmptyArray(params.siteIDs)) {
filters.siteID = {
- $in: params.siteIDs.map((site) => Utils.convertToObjectID(site))
+ $in: params.siteIDs.map((siteID) => Utils.convertToObjectID(siteID))
};
}
if (params.issuer === true || params.issuer === false) {

MODIFY
src/storage/mongodb/SiteStorage.ts
src/storage/mongodb/SiteStorage.ts
@@ -317,8 +317,14 @@ export default class SiteStorage {
{ 'name': { $regex: Utils.escapeSpecialCharsInRegex(params.search), $options: 'i' } }
];
}
- // Query by companyIDs
- if (params.companyIDs && Array.isArray(params.companyIDs) && params.companyIDs.length > 0) {
+ // Site
+ if (!Utils.isEmptyArray(params.siteIDs)) {
+ filters._id = {
+ $in: params.siteIDs.map((siteID) => Utils.convertToObjectID(siteID))
+ };
+ }
+ // Company
+ if (!Utils.isEmptyArray(params.companyIDs)) {
filters.companyID = {
$in: params.companyIDs.map((company) => Utils.convertToObjectID(company))
};
@@ -330,14 +336,6 @@ export default class SiteStorage {
if (params.withAutoUserAssignment) {
filters.autoUserSiteAssignment = true;
}
- // Limit on Site for Basic Users
- if (params.siteIDs && params.siteIDs.length > 0) {
- aggregation.push({
- $match: {
- _id: { $in: params.siteIDs.map((siteID) => Utils.convertToObjectID(siteID)) }
- }
- });
- }
// Get users
if (params.userID || params.excludeSitesOfUserID) {
DatabaseUtils.pushCollectionLookupInAggregation('siteusers',

MODIFY
src/storage/mongodb/StatisticsStorage.ts
src/storage/mongodb/StatisticsStorage.ts
@@ -27,30 +27,30 @@ export default class StatisticsStorage {
if (filters.endDateTime) {
match.timestamp.$lte = Utils.convertToDate(filters.endDateTime);
}
- // Check stop tr
+ // Check stop transaction
if (filters.stop) {
match.stop = filters.stop;
}
- // Filter on Site?
- if (filters.siteIDs && Array.isArray(filters.siteIDs) && filters.siteIDs.length > 0) {
+ // Site
+ if (!Utils.isEmptyArray(filters.siteIDs)) {
match.siteID = {
$in: filters.siteIDs.map((siteID) => Utils.convertToObjectID(siteID))
};
}
// Filter on Site Area?
- if (filters.siteAreaIDs && Array.isArray(filters.siteAreaIDs) && filters.siteAreaIDs.length > 0) {
+ if (!Utils.isEmptyArray(filters.siteAreaIDs)) {
match.siteAreaID = {
$in: filters.siteAreaIDs.map((siteAreaID) => Utils.convertToObjectID(siteAreaID))
};
}
// Filter on Charge Box?
- if (filters.chargeBoxIDs && Array.isArray(filters.chargeBoxIDs) && filters.chargeBoxIDs.length > 0) {
+ if (!Utils.isEmptyArray(filters.chargeBoxIDs)) {
match.chargeBoxID = {
$in: filters.chargeBoxIDs.map((chargeBoxID) => chargeBoxID)
};
}
// Filter on User?
- if (filters.userIDs && Array.isArray(filters.userIDs) && filters.userIDs.length > 0) {
+ if (!Utils.isEmptyArray(filters.userIDs)) {
match.userID = {
$in: filters.userIDs.map((userID) => Utils.convertToObjectID(userID))
};
@@ -153,25 +153,25 @@ export default class StatisticsStorage {
match.stop = filters.stop;
}
// Filter on Site?
- if (filters.siteIDs && Array.isArray(filters.siteIDs) && filters.siteIDs.length > 0) {
+ if (!Utils.isEmptyArray(filters.siteIDs)) {
match.siteID = {
$in: filters.siteIDs.map((siteID) => Utils.convertToObjectID(siteID))
};
}
// Filter on Site Area?
- if (filters.siteAreaIDs && Array.isArray(filters.siteAreaIDs) && filters.siteAreaIDs.length > 0) {
+ if (!Utils.isEmptyArray(filters.siteAreaIDs)) {
match.siteAreaID = {
$in: filters.siteAreaIDs.map((siteAreaID) => Utils.convertToObjectID(siteAreaID))
};
}
// Filter on Charge Box?
- if (filters.chargeBoxIDs && Array.isArray(filters.chargeBoxIDs) && filters.chargeBoxIDs.length > 0) {
+ if (!Utils.isEmptyArray(filters.chargeBoxIDs)) {
match.chargeBoxID = {
$in: filters.chargeBoxIDs.map((chargeBoxID) => chargeBoxID)
};
}
// Filter on User?
- if (filters.userIDs && Array.isArray(filters.userIDs) && filters.userIDs.length > 0) {
+ if (!Utils.isEmptyArray(filters.userIDs)) {
match.userID = {
$in: filters.userIDs.map((userID) => Utils.convertToObjectID(userID))
};

MODIFY
src/storage/mongodb/TenantStorage.ts
src/storage/mongodb/TenantStorage.ts
@@ -18,7 +18,7 @@ export default class TenantStorage {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getTenant');
// Delegate querying
- const tenantsMDB = await TenantStorage.getTenants({ tenantID: id }, Constants.DB_PARAMS_SINGLE_RECORD);
+ const tenantsMDB = await TenantStorage.getTenants({ tenantIDs: [id] }, Constants.DB_PARAMS_SINGLE_RECORD);
// Debug
Logging.traceEnd(MODULE_NAME, 'getTenant', uniqueTimerID, { id });
return tenantsMDB.count > 0 ? tenantsMDB.result[0] : null;
@@ -96,7 +96,7 @@ export default class TenantStorage {
// Delegate
public static async getTenants(
- params: { tenantID?: string; tenantName?: string; tenantSubdomain?: string; search?: string },
+ params: { tenantIDs?: string[]; tenantName?: string; tenantSubdomain?: string; search?: string },
dbParams: DbParams, projectFields?: string[]): Promise<DataResult<Tenant>> {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getTenants');
@@ -106,15 +106,19 @@ export default class TenantStorage {
dbParams.skip = Utils.checkRecordSkip(dbParams.skip);
// Set the filters
const filters: any = {};
- if (params.tenantID) {
- filters._id = Utils.convertToObjectID(params.tenantID);
- } else if (params.search) {
+ if (params.search) {
const searchRegex = Utils.escapeSpecialCharsInRegex(params.search);
filters.$or = [
{ 'name': { $regex: searchRegex, $options: 'i' } },
{ 'subdomain': { $regex: searchRegex, $options: 'i' } }
];
}
+ // Tenant
+ if (!Utils.isEmptyArray(params.tenantIDs)) {
+ filters._id = {
+ $in: params.tenantIDs.map((tenantID) => Utils.convertToObjectID(tenantID))
+ };
+ }
// Name
if (params.tenantName) {
filters.name = params.tenantName;

MODIFY
src/storage/mongodb/TransactionStorage.ts
src/storage/mongodb/TransactionStorage.ts
@@ -250,7 +250,7 @@ export default class TransactionStorage {
public static async getTransactions(tenantID: string,
params: {
- transactionId?: number; issuer?: boolean; search?: string; ownerID?: string; userIDs?: string[]; siteAdminIDs?: string[];
+ transactionIDs?: number[]; issuer?: boolean; search?: string; ownerID?: string; userIDs?: string[]; siteAdminIDs?: string[];
chargeBoxIDs?: string[]; siteAreaIDs?: string[]; siteIDs?: string[]; connectorId?: number; startDateTime?: Date;
endDateTime?: Date; stop?: any; minimalPrice?: boolean; reportIDs?: string[]; inactivityStatus?: string[];
ocpiSessionId?: string; ocpiSessionDateFrom?: Date; ocpiSessionDateTo?: Date; ocpiCdrDateFrom?: Date; ocpiCdrDateTo?: Date;
@@ -275,7 +275,7 @@ export default class TransactionStorage {
dbParams.skip = Utils.checkRecordSkip(dbParams.skip);
// Build filter
const ownerMatch = { $or: [] };
- const filterMatch: any = {};
+ const filters: any = {};
// User / Site Admin
if (params.ownerID) {
ownerMatch.$or.push({
@@ -290,110 +290,114 @@ export default class TransactionStorage {
});
}
// Filter?
- if (params.transactionId) {
- filterMatch._id = params.transactionId;
- } else if (params.ocpiSessionId) {
- filterMatch['ocpiData.session.id'] = params.ocpiSessionId;
+ if (params.ocpiSessionId) {
+ filters['ocpiData.session.id'] = params.ocpiSessionId;
} else if (params.search) {
// Build filter
- filterMatch.$or = [
+ filters.$or = [
{ '_id': Utils.convertToInt(params.search) },
{ 'tagID': { $regex: params.search, $options: 'i' } },
{ 'chargeBoxID': { $regex: params.search, $options: 'i' } }
];
}
+ // Transaction
+ if (!Utils.isEmptyArray(params.transactionIDs)) {
+ filters._id = {
+ $in: params.transactionIDs
+ };
+ }
// Issuer
if (params.issuer === true || params.issuer === false) {
- filterMatch.issuer = params.issuer;
+ filters.issuer = params.issuer;
}
// Charge Box
if (params.userIDs) {
- filterMatch.userID = { $in: params.userIDs.map((siteID) => Utils.convertToObjectID(siteID)) };
+ filters.userID = { $in: params.userIDs.map((siteID) => Utils.convertToObjectID(siteID)) };
}
// Charge Box
if (params.chargeBoxIDs) {
- filterMatch.chargeBoxID = { $in: params.chargeBoxIDs };
+ filters.chargeBoxID = { $in: params.chargeBoxIDs };
}
// Connector
if (params.connectorId) {
- filterMatch.connectorId = Utils.convertToInt(params.connectorId);
+ filters.connectorId = Utils.convertToInt(params.connectorId);
}
// Date provided?
if (params.startDateTime || params.endDateTime) {
- filterMatch.timestamp = {};
+ filters.timestamp = {};
// Start date
if (params.startDateTime) {
- filterMatch.timestamp.$gte = Utils.convertToDate(params.startDateTime);
+ filters.timestamp.$gte = Utils.convertToDate(params.startDateTime);
}
// End date
if (params.endDateTime) {
- filterMatch.timestamp.$lte = Utils.convertToDate(params.endDateTime);
+ filters.timestamp.$lte = Utils.convertToDate(params.endDateTime);
}
}
// OCPI Session Date provided?
if (params.ocpiSessionDateFrom || params.ocpiSessionDateTo) {
// Start date
if (params.ocpiSessionDateFrom) {
- filterMatch['ocpiData.session.last_updated'] = { $gte: Utils.convertToDate(params.ocpiSessionDateFrom) };
+ filters['ocpiData.session.last_updated'] = { $gte: Utils.convertToDate(params.ocpiSessionDateFrom) };
}
// End date
if (params.ocpiSessionDateTo) {
- filterMatch['ocpiData.session.last_updated'] = { $lte: Utils.convertToDate(params.ocpiSessionDateTo) };
+ filters['ocpiData.session.last_updated'] = { $lte: Utils.convertToDate(params.ocpiSessionDateTo) };
}
}
if (params.ocpiSessionChecked === true || params.ocpiSessionChecked === false) {
- filterMatch['ocpiData.session'] = { $exists: true };
- filterMatch['ocpiData.sessionCheckedOn'] = { $exists: params.ocpiSessionChecked };
+ filters['ocpiData.session'] = { $exists: true };
+ filters['ocpiData.sessionCheckedOn'] = { $exists: params.ocpiSessionChecked };
}
// OCPI Cdr Date provided?
if (params.ocpiCdrDateFrom || params.ocpiCdrDateTo) {
// Start date
if (params.ocpiCdrDateFrom) {
- filterMatch['ocpiData.cdr.last_updated'] = { $gte: Utils.convertToDate(params.ocpiCdrDateFrom) };
+ filters['ocpiData.cdr.last_updated'] = { $gte: Utils.convertToDate(params.ocpiCdrDateFrom) };
}
// End date
if (params.ocpiCdrDateTo) {
- filterMatch['ocpiData.cdr.last_updated'] = { $lte: Utils.convertToDate(params.ocpiCdrDateTo) };
+ filters['ocpiData.cdr.last_updated'] = { $lte: Utils.convertToDate(params.ocpiCdrDateTo) };
}
}
if (params.ocpiCdrChecked === true || params.ocpiCdrChecked === false) {
- filterMatch['ocpiData.cdr'] = { $exists: true };
- filterMatch['ocpiData.cdrCheckedOn'] = { $exists: params.ocpiCdrChecked };
+ filters['ocpiData.cdr'] = { $exists: true };
+ filters['ocpiData.cdrCheckedOn'] = { $exists: params.ocpiCdrChecked };
}
// Check stop transaction
if (params.stop) {
- filterMatch.stop = params.stop;
+ filters.stop = params.stop;
}
// Inactivity Status
if (params.inactivityStatus) {
- filterMatch['stop.inactivityStatus'] = { $in: params.inactivityStatus };
+ filters['stop.inactivityStatus'] = { $in: params.inactivityStatus };
}
// Site's area ID
if (params.siteAreaIDs) {
- filterMatch.siteAreaID = {
+ filters.siteAreaID = {
$in: params.siteAreaIDs.map((siteAreaID) => Utils.convertToObjectID(siteAreaID))
};
}
// Site ID
if (params.siteIDs) {
- filterMatch.siteID = {
+ filters.siteID = {
$in: params.siteIDs.map((siteID) => Utils.convertToObjectID(siteID))
};
}
// Refund status
if (params.refundStatus && params.refundStatus.length > 0) {
const statuses = params.refundStatus.map((status) => status === RefundStatus.NOT_SUBMITTED ? null : status);
- filterMatch['refundData.status'] = {
+ filters['refundData.status'] = {
$in: statuses
};
}
// Minimal Price
if (params.minimalPrice) {
- filterMatch['stop.price'] = { $gt: Utils.convertToInt(params.minimalPrice) };
+ filters['stop.price'] = { $gt: Utils.convertToInt(params.minimalPrice) };
}
// Report ID
if (params.reportIDs) {
- filterMatch['refundData.reportId'] = { $in: params.reportIDs };
+ filters['refundData.reportId'] = { $in: params.reportIDs };
}
// Create Aggregation
const aggregation = [];
@@ -401,12 +405,12 @@ export default class TransactionStorage {
if (ownerMatch.$or && ownerMatch.$or.length > 0) {
aggregation.push({
$match: {
- $and: [ ownerMatch, filterMatch ]
+ $and: [ ownerMatch, filters ]
}
});
} else {
aggregation.push({
- $match: filterMatch
+ $match: filters
});
}
// Limit records?
@@ -578,7 +582,7 @@ export default class TransactionStorage {
public static async getRefundReports(tenantID: string,
params: { ownerID?: string; siteAdminIDs?: string[] },
- dbParams: DbParams, projectFields?: string[]): Promise<{ count: number; result: RefundReport[]; stats: {} }> {
+ dbParams: DbParams, projectFields?: string[]): Promise<{ count: number; result: RefundReport[]}> {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getTransactions');
// Check
@@ -590,8 +594,8 @@ export default class TransactionStorage {
// Create Aggregation
const aggregation = [];
const ownerMatch = { $or: [] };
- const filterMatch = {};
- filterMatch['refundData.reportId'] = { '$ne': null };
+ const filters = {};
+ filters['refundData.reportId'] = { '$ne': null };
if (params.ownerID) {
ownerMatch.$or.push({
userID: Utils.convertToObjectID(params.ownerID)
@@ -608,13 +612,13 @@ export default class TransactionStorage {
aggregation.push({
$match: {
$and: [
- ownerMatch, filterMatch
+ ownerMatch, filters
]
}
});
} else {
aggregation.push({
- $match: filterMatch
+ $match: filters
});
}
aggregation.push({
@@ -650,7 +654,6 @@ export default class TransactionStorage {
if (dbParams.onlyRecordCount) {
return {
count: reportCountMDB ? reportCountMDB.count : 0,
- stats: reportCountMDB ? reportCountMDB : {},
result: []
};
}
@@ -707,7 +710,6 @@ export default class TransactionStorage {
Logging.traceEnd(MODULE_NAME, 'getRefundReports', uniqueTimerID, { dbParams });
return {
count: reportCountMDB ? (reportCountMDB.count === Constants.DB_RECORD_COUNT_CEIL ? -1 : reportCountMDB.count) : 0,
- stats: reportCountMDB ? reportCountMDB : {},
result: reportsMDB
};
}
@@ -802,7 +804,7 @@ export default class TransactionStorage {
});
}
// Build facets for each type of error if any
- if (params.errorType && Array.isArray(params.errorType) && params.errorType.length > 0) {
+ if (!Utils.isEmptyArray(params.errorType)) {
const facets: any = { $facet: {} };
const array = [];
params.errorType.forEach((type) => {
@@ -873,7 +875,7 @@ export default class TransactionStorage {
// Check
await Utils.checkTenant(tenantID);
// Delegate work
- const transactionsMDB = await TransactionStorage.getTransactions(tenantID, { transactionId: id }, Constants.DB_PARAMS_SINGLE_RECORD);
+ const transactionsMDB = await TransactionStorage.getTransactions(tenantID, { transactionIDs: [id] }, Constants.DB_PARAMS_SINGLE_RECORD);
// Debug
Logging.traceEnd(MODULE_NAME, 'getTransaction', uniqueTimerID, { id });
// Found?

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -529,7 +529,7 @@ export default class UserStorage {
filters.passwordResetHash = params.passwordResetHash;
}
// Role
- if (params.roles && Array.isArray(params.roles) && params.roles.length > 0) {
+ if (!Utils.isEmptyArray(params.roles)) {
filters.role = { $in: params.roles };
}
// Billing Customer

MODIFY
src/utils/Utils.ts
src/utils/Utils.ts
@@ -830,6 +830,9 @@ export default class Utils {
}
public static isEmptyArray(array: any): boolean {
+ if (!array) {
+ return true;
+ }
if (Array.isArray(array) && array.length > 0) {
return false;
}

Use of utils array check + use array of IDs to get a single record