Skip to content

Commit

Permalink
update atribute on input and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
WisnuDS committed Dec 26, 2021
1 parent 9fcf108 commit d81055e
Show file tree
Hide file tree
Showing 8 changed files with 4,196 additions and 113 deletions.
Binary file added .DS_Store
Binary file not shown.
57 changes: 36 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
const { Op } = require("sequelize");
const moment = require('moment')
function convert(clause) {
if (clause.type === 'filter') return generateFilter(clause)
if (clause.group.length === 0) return generateFilter(clause)
else return generateGroupFilter(clause)
}

function generateFilter(filter) {
switch (filter.operator){
case 'contains' : return {[filter.variable] : {[Op.like]: `%${filter.value}%`}}
case 'does_not_contains' : return {[filter.variable] : {[Op.notLike]: `%${filter.value}%`}}
case 'is' : return {[filter.variable] : filter.value}
case 'is_not' : return {[filter.variable] : {[Op.not]: filter.value}}
case 'start_with' : return {[filter.variable] : {[Op.like]: `${filter.value}%`}}
case 'end_with' : return {[filter.variable] : {[Op.like]: `%${filter.value}`}}
case '=' : return {[filter.variable] : {[Op.eq]: filter.value}}
case '<' : return {[filter.variable] : {[Op.lt]: filter.value}}
case '>' : return {[filter.variable] : {[Op.gt]: filter.value}}
case '<=' : return {[filter.variable] : {[Op.lte]: filter.value}}
case '>=' : return {[filter.variable] : {[Op.gte]: filter.value}}
case '<>' : return {[filter.variable] : {[Op.ne]: filter.value}}
case 'between' : return {[filter.variable] : {[Op.between]: filter.value}}
case 'at' : return {[filter.variable] : filter.value}
case 'at_between' : return {[filter.variable] : {[Op.between]: filter.value}}
case 'before' : return {[filter.variable] : {[Op.lt]: filter.value}}
case 'after' : return {[filter.variable] : {[Op.gt]: filter.value}}
switch (filter.function_name){
case 'contains' : return {[filter.column] : {[Op.like]: `%${filter.value}%`}}
case 'does_not_contains' : return {[filter.column] : {[Op.notLike]: `%${filter.value}%`}}
case 'is' : return {[filter.column] : filter.value}
case 'is_not' : return {[filter.column] : {[Op.not]: filter.value}}
case 'start_with' : return {[filter.column] : {[Op.like]: `${filter.value}%`}}
case 'end_with' : return {[filter.column] : {[Op.like]: `%${filter.value}`}}
case '=' : return {[filter.column] : {[Op.eq]: filter.value}}
case '<' : return {[filter.column] : {[Op.lt]: filter.value}}
case '>' : return {[filter.column] : {[Op.gt]: filter.value}}
case '<=' : return {[filter.column] : {[Op.lte]: filter.value}}
case '>=' : return {[filter.column] : {[Op.gte]: filter.value}}
case '<>' : return {[filter.column] : {[Op.ne]: filter.value}}
case 'between' : return {[filter.column] : {[Op.between]: filter.value}}
case 'at' : return {[filter.column] : convertToDate(filter.value)}
case 'at_between' : return {[filter.column] : {[Op.between]: convertToDate(filter.value)}}
case 'before' : return {[filter.column] : {[Op.lt]: convertToDate(filter.value)}}
case 'after' : return {[filter.column] : {[Op.gt]: convertToDate(filter.value)}}
default : throw Error("ERROR : Operator not found")
}
}
Expand All @@ -35,7 +36,7 @@ function generateGroupFilter(groupFilter) {
let structure = {}
let group = []
for (const groupFilterElement of groupFilter.filters) {
if (groupFilterElement.type === 'filter'){
if (groupFilterElement.group.length === 0){
let filter = generateFilter(groupFilterElement)
if (!filter) throw Error("ERROR : Filter is empty")
group.push(filter)
Expand All @@ -45,7 +46,7 @@ function generateGroupFilter(groupFilter) {
}
}

if (groupFilter.logical_operator === 'AND'){
if (groupFilter.operator === 'AND'){
structure[Op.and] = group
} else {
structure[Op.or] = group
Expand All @@ -54,4 +55,18 @@ function generateGroupFilter(groupFilter) {
return structure
}

function convertToDate (values) {
if (typeof values === 'string'){
return moment.utc(values).toDate()
}

const converted = []
for (const value of values) {
converted.push(moment.utc(value).toDate())
}

return converted
}

module.exports = convert

Loading

0 comments on commit d81055e

Please sign in to comment.