@giftwizard/db-models
Advanced tools
Comparing version 8.0.0 to 8.0.1
@@ -41,7 +41,7 @@ module.exports = (sequelizeConnection, DataTypes, Sequelize) => { | ||
const offset = parseInt(page) * limitInt - limitInt; | ||
try { | ||
const { rows, count } = await Customer.findAndCountAll({ | ||
const result = await Customer.findAll({ | ||
limit: limitInt, | ||
offset, | ||
attributes: ['email', 'first_name', 'last_name', 'state'], | ||
include: [ | ||
@@ -53,2 +53,3 @@ { | ||
{ | ||
attributes: ['balance', 'code'], | ||
model: sequelizeConnection.models.GiftCard, | ||
@@ -61,8 +62,12 @@ required: true | ||
where: { | ||
shop_url | ||
shop_url, | ||
loyalty_card_id: { | ||
[Op.ne]: null, | ||
} | ||
}, | ||
order: [[sequelizeConnection.models.LoyaltyCard, { model: sequelizeConnection.models.GiftCard }, 'balance', 'desc']] | ||
order: [['updated_at', 'DESC']] | ||
}); | ||
return { rows, count }; | ||
return { rows: result, count: 100 }; | ||
} catch (error) { | ||
@@ -157,2 +162,78 @@ throw new Error(`Unable to get all customers page - ${page} for shop - ${shop_url}, ${error}`); | ||
Customer.getCustomersSearchByEmail = (shop_url, search) => { | ||
const sanitizedSearchQuery = search.trim().toLowerCase(); | ||
const searchQueryTokens = sanitizedSearchQuery.split(/\s+/); | ||
const fields = ['email']; | ||
const operatorsFields = fields.map(field => { | ||
return { | ||
[Op.or]: searchQueryTokens.map(token => | ||
sequelizeConnection.where(sequelizeConnection.fn('lower', sequelizeConnection.col(field)), 'LIKE', `%${token}%`) | ||
) | ||
}; | ||
}); | ||
return Customer.findAll({ | ||
limit: 20, | ||
where: { | ||
shop_url, | ||
[Op.or]: operatorsFields | ||
}, | ||
include: [ | ||
{ | ||
model: sequelizeConnection.models.LoyaltyCard, | ||
include: [ | ||
{ | ||
model: sequelizeConnection.models.GiftCard | ||
} | ||
] | ||
} | ||
] | ||
}) | ||
.then(customers => { | ||
return customers; | ||
}) | ||
.catch(error => { | ||
throw new Error(`Unable to search customers for shop - ${shop_url}, ${error}`); | ||
}); | ||
}; | ||
Customer.getCustomersSearchByName = (shop_url, search) => { | ||
const sanitizedSearchQuery = search.trim().toLowerCase(); | ||
const searchQueryTokens = sanitizedSearchQuery.split(/\s+/); | ||
const fields = ['first_name', 'last_name']; | ||
const operatorsFields = fields.map(field => { | ||
return { | ||
[Op.or]: searchQueryTokens.map(token => | ||
sequelizeConnection.where(sequelizeConnection.fn('lower', sequelizeConnection.col(field)), 'LIKE', `%${token}%`) | ||
) | ||
}; | ||
}); | ||
return Customer.findAll({ | ||
limit: 20, | ||
where: { | ||
shop_url, | ||
[Op.or]: operatorsFields | ||
}, | ||
include: [ | ||
{ | ||
model: sequelizeConnection.models.LoyaltyCard, | ||
include: [ | ||
{ | ||
model: sequelizeConnection.models.GiftCard | ||
} | ||
] | ||
} | ||
] | ||
}) | ||
.then(customers => { | ||
return customers; | ||
}) | ||
.catch(error => { | ||
throw new Error(`Unable to search customers for shop - ${shop_url}, ${error}`); | ||
}); | ||
}; | ||
Customer.updateLoyaltyCard = (shop_url, customerId, giftCardId) => { | ||
@@ -175,3 +256,3 @@ return sequelizeConnection.models.LoyaltyCard.create({ gift_card_id: giftCardId }) | ||
} | ||
).then(() => {}); | ||
).then(() => { }); | ||
}) | ||
@@ -185,1 +266,2 @@ .catch(error => { | ||
}; | ||
{ | ||
"name": "@giftwizard/db-models", | ||
"version": "8.0.0", | ||
"version": "8.0.1", | ||
"description": "Rise.ai package for 'sequelize' models used by several projects.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
86996
45
3252