@avonjs/avonjs
Version:
A fluent Node.js API generator.
70 lines (69 loc) • 2.28 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Contracts_1 = require("../Contracts");
const Filter_1 = __importDefault(require("./Filter"));
class DateTimeFilter extends Filter_1.default {
/**
* Apply the filter into the given repository.
*/
apply(request, repository, dates) {
if (typeof dates !== 'object') {
return;
}
if (dates.from !== undefined) {
repository.where({
key: this.filterableAttribute(request),
operator: this.isValidNullValue(dates.from)
? Contracts_1.Operator.eq
: Contracts_1.Operator.gte,
value: this.isValidNullValue(dates.from) ? null : dates.from,
});
}
if (dates.to !== undefined) {
repository.where({
key: this.filterableAttribute(request),
operator: this.isValidNullValue(dates.to) ? Contracts_1.Operator.eq : Contracts_1.Operator.lte,
value: this.isValidNullValue(dates.to) ? null : dates.to,
});
}
}
/**
* Serialize parameters for schema.
*/
serializeParameters(request) {
return [
{
name: `filters[${this.key()}][from]`,
in: 'query',
explode: true,
style: 'deepObject',
description: this.helpText,
allowEmptyValue: this.isNullable(),
schema: this.schema(request),
},
{
name: `filters[${this.key()}][to]`,
in: 'query',
explode: true,
style: 'deepObject',
description: this.helpText,
allowEmptyValue: this.isNullable(),
schema: this.schema(request),
},
];
}
/**
* Get the swagger-ui schema.
*/
schema(request) {
return {
type: 'string',
nullable: this.isNullable(),
format: 'date-time',
};
}
}
exports.default = DateTimeFilter;