Skip to content

Commit

Permalink
fix query
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Jan 24, 2025
1 parent a42c73b commit e216fc9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export class DeletionExecutionConsole {
{
flags: '-l, --limit <value>',
/* istanbul ignore next */
fn: (value: string) => (value ? Number(value) : undefined),
fn: (value: string) => (value ? Number(value) : undefined), // NOSONAR
description: 'Limit of the requested deletion executions that should be performed.',
required: false,
},
{
flags: '-f, --runFailed <value>',
/* istanbul ignore next */
fn: (value: string) => /^(true|yes|1)$/i.test(value),
fn: (value: string) => /^(true|yes|1)$/i.test(value), // NOSONAR
description: 'Limit of the requested deletion executions that should be performed.',
required: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,54 @@ describe(DeletionRequestScope.name, () => {
status: {
$in: [StatusModel.FAILED, StatusModel.PENDING],
},
updatedAt: {
$lt: olderThan,
},
$and: [
{
updatedAt: {
$lt: olderThan,
},
},
],
};
const expectedQueryNewer = {
status: {
$in: [StatusModel.FAILED, StatusModel.PENDING],
},
updatedAt: {
$gte: newerThan,
$and: [
{
updatedAt: {
$gte: newerThan,
},
},
],
};
const expectedQueryOlderAndNewer = {
status: {
$in: [StatusModel.FAILED, StatusModel.PENDING],
},
$and: [
{
updatedAt: {
$lt: olderThan,
},
},
{
updatedAt: {
$gte: newerThan,
},
},
],
};
const expectedQueryNoDates = {
status: {
$in: [StatusModel.FAILED, StatusModel.PENDING],
},
};

return {
expectedQueryOlder,
expectedQueryNewer,
expectedQueryOlderAndNewer,
expectedQueryNoDates,
olderThan,
newerThan,
};
Expand All @@ -80,6 +113,26 @@ describe(DeletionRequestScope.name, () => {
expect(scope.query).toEqual(expectedQueryNewer);
});
});
describe('when olderThan and newerThan are set', () => {
it('should add query', () => {
const { expectedQueryOlderAndNewer, olderThan, newerThan } = setup();

const result = scope.byStatusAndDate([StatusModel.FAILED, StatusModel.PENDING], olderThan, newerThan);

expect(result).toBeInstanceOf(DeletionRequestScope);
expect(scope.query).toEqual(expectedQueryOlderAndNewer);
});
});
describe('when olderThan and newerThan are not set', () => {
it('should add query', () => {
const { expectedQueryNoDates } = setup();

const result = scope.byStatusAndDate([StatusModel.FAILED, StatusModel.PENDING]);

expect(result).toBeInstanceOf(DeletionRequestScope);
expect(scope.query).toEqual(expectedQueryNoDates);
});
});
});

describe('byStatusAndDate for Pending', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Scope } from '@shared/repo/scope';
import { FilterQuery } from '@mikro-orm/core';
import { DeletionRequestEntity } from '../entity';
import { StatusModel } from '../../domain/types';

Expand All @@ -10,19 +11,21 @@ export class DeletionRequestScope extends Scope<DeletionRequestEntity> {
}

byStatusAndDate(status: StatusModel[], olderThan?: Date, newerThan?: Date): this {
let query = { status: { $in: status } };
const query: FilterQuery<DeletionRequestEntity> = { status: { $in: status } };

const dateConditions: FilterQuery<DeletionRequestEntity>[] = [];
if (olderThan) {
const olderThanQuery = { updatedAt: { $lt: olderThan } };
query = { ...query, ...olderThanQuery };
dateConditions.push({ updatedAt: { $lt: olderThan } });
}

if (newerThan) {
const newerThanQuery = { updatedAt: { $gte: newerThan } };
query = { ...query, ...newerThanQuery };
dateConditions.push({ updatedAt: { $gte: newerThan } });
}

this.addQuery(query);
if (dateConditions.length > 0) {
query.$and = dateConditions;
}

this.addQuery(query);
return this;
}
}

0 comments on commit e216fc9

Please sign in to comment.