feat: use customer Autocomplete on discount creation
This commit is contained in:
parent
43ff04b2af
commit
847b87ecb0
8 changed files with 94 additions and 189 deletions
|
|
@ -32,20 +32,14 @@ function getAvailableIndividualDiscounts () {
|
|||
const sql = `SELECT * from individual_discounts WHERE soft_deleted=false`
|
||||
return db.any(sql).then(res => _.map(it => ({
|
||||
id: it.id,
|
||||
idType: _.camelCase(it.identification),
|
||||
value: it.value,
|
||||
customerId: it.customer_id,
|
||||
discount: it.discount
|
||||
}), res))
|
||||
}
|
||||
|
||||
function createIndividualDiscount (idType, value, discount) {
|
||||
const idTypes = {
|
||||
phone: 'phone',
|
||||
idNumber: 'id_number'
|
||||
}
|
||||
|
||||
const sql = `INSERT INTO individual_discounts (id, identification, value, discount) VALUES ($1, $2, $3, $4)`
|
||||
return db.none(sql, [uuid.v4(), idTypes[idType], value, discount])
|
||||
function createIndividualDiscount (customerId, discount) {
|
||||
const sql = `INSERT INTO individual_discounts (id, customer_id, discount) VALUES ($1, $2, $3)`
|
||||
return db.none(sql, [uuid.v4(), customerId, discount])
|
||||
}
|
||||
|
||||
function deleteIndividualDiscount (id) {
|
||||
|
|
@ -53,38 +47,6 @@ function deleteIndividualDiscount (id) {
|
|||
return db.none(sql, [id])
|
||||
}
|
||||
|
||||
function getCustomersWithDiscounts (discounts) {
|
||||
let phoneNumbers = []
|
||||
let idCardNumbers = []
|
||||
|
||||
_.each(it => {
|
||||
switch (it.idType) {
|
||||
case 'phone':
|
||||
phoneNumbers.push(it.value)
|
||||
break
|
||||
case 'idNumber':
|
||||
idCardNumbers.push(it.value)
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
}, discounts)
|
||||
|
||||
if (_.isEmpty(phoneNumbers) && _.isEmpty(idCardNumbers)) {
|
||||
return Promise.resolve([])
|
||||
}
|
||||
|
||||
const phoneNumbersSql = _.map(pgp.as.text, phoneNumbers).join(',')
|
||||
const idCardNumbersSql = _.map(pgp.as.text, idCardNumbers).join(',')
|
||||
|
||||
const hasPhoneNumbers = !_.isEmpty(phoneNumbers)
|
||||
const hasIDNumbers = !_.isEmpty(idCardNumbers)
|
||||
|
||||
const sql = `SELECT * FROM customers WHERE ${hasPhoneNumbers ? `phone IN ($1^)` : ``} ${hasPhoneNumbers && hasIDNumbers ? `OR` : ``} ${hasIDNumbers ? `id_card_data_number IN ($2^)` : ``}`
|
||||
return db.any(sql, [phoneNumbersSql, idCardNumbersSql])
|
||||
.then(res => _.map(it => it ? _.mapKeys(_.camelCase, it) : null, res))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAvailablePromoCodes,
|
||||
getPromoCode,
|
||||
|
|
@ -93,6 +55,5 @@ module.exports = {
|
|||
getNumberOfAvailablePromoCodes,
|
||||
getAvailableIndividualDiscounts,
|
||||
createIndividualDiscount,
|
||||
deleteIndividualDiscount,
|
||||
getCustomersWithDiscounts
|
||||
deleteIndividualDiscount
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ const resolvers = {
|
|||
Mutation: {
|
||||
createPromoCode: (...[, { code, discount }]) => loyalty.createPromoCode(code, discount),
|
||||
deletePromoCode: (...[, { codeId }]) => loyalty.deletePromoCode(codeId),
|
||||
createIndividualDiscount: (...[, { idType, value, discount }]) => loyalty.createIndividualDiscount(idType, value, discount),
|
||||
createIndividualDiscount: (...[, { customerId, discount }]) => loyalty.createIndividualDiscount(customerId, discount),
|
||||
deleteIndividualDiscount: (...[, { discountId }]) => loyalty.deleteIndividualDiscount(discountId)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,23 +3,10 @@ const { gql } = require('apollo-server-express')
|
|||
const typeDef = gql`
|
||||
type IndividualDiscount {
|
||||
id: ID!
|
||||
idType: DiscountIdentificationType
|
||||
value: String!
|
||||
customerId: ID!
|
||||
discount: Int
|
||||
}
|
||||
|
||||
input IndividualDiscountInput {
|
||||
id: ID
|
||||
idType: DiscountIdentificationType
|
||||
value: String
|
||||
discount: Int
|
||||
}
|
||||
|
||||
enum DiscountIdentificationType {
|
||||
phone
|
||||
idNumber
|
||||
}
|
||||
|
||||
type PromoCode {
|
||||
id: ID!
|
||||
code: String!
|
||||
|
|
@ -34,7 +21,7 @@ const typeDef = gql`
|
|||
type Mutation {
|
||||
createPromoCode(code: String!, discount: Int!): PromoCode @auth
|
||||
deletePromoCode(codeId: ID!): PromoCode @auth
|
||||
createIndividualDiscount(idType: DiscountIdentificationType!, value: String!, discount: Int!): IndividualDiscount @auth
|
||||
createIndividualDiscount(customerId: ID!, discount: Int!): IndividualDiscount @auth
|
||||
deleteIndividualDiscount(discountId: ID!): IndividualDiscount @auth
|
||||
}
|
||||
`
|
||||
|
|
|
|||
|
|
@ -260,6 +260,8 @@ function plugins (settings, deviceId) {
|
|||
const areThereAvailablePromoCodes = arr[arr.length - 2] > 0
|
||||
const individualDiscounts = arr[arr.length - 1]
|
||||
|
||||
console.log('individualDiscounts', individualDiscounts)
|
||||
|
||||
return {
|
||||
cassettes,
|
||||
rates: buildRates(tickers),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue