-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathph-address-finder.js
161 lines (147 loc) · 4.94 KB
/
ph-address-finder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//// Core modules
const path = require('path')
//// External modules
const { Op } = require('sequelize');
//// Modules
class PhAddressFinder {
#sequelize
#Address;
constructor(sequelize, Address) {
this.#sequelize = sequelize
this.#Address = Address
}
async find(search, minChar = 3, limit = 50, formatter = null) {
try {
if(!this.#sequelize){
throw new Error('Please use connect() first.')
}
let addresses = []
if (search.length < minChar) {
return addresses
}
let keys = search.split(',')
keys = keys.map((o) => {
o = o.trim()
return o
})
if (keys.length === 1) {
// Find all brgys that match or starts with keys[0]
addresses = await this.#Address.findAll({
where: {
name: {
[Op.like]: keys[0],
},
level: 'Bgy',
},
raw: true,
order: ['name', 'cityMunName']
})
if (addresses.length <= 0) {
// Broader search criteria
let criteria = this.#expandNameCriteria(keys[0])
addresses = await this.#Address.findAll({
where: {
level: 'Bgy',
[Op.or]: criteria
},
limit: limit,
raw: true,
order: ['name', 'cityMunName']
})
}
} else if (keys.length === 2) {
let criteria = this.#expandNameCriteria(keys[0])
let where = {
[Op.or]: criteria,
cityMunName: {
[Op.or]: {
[Op.like]: keys[1],
[Op.like]: keys[1] + '%',
}
}
}
addresses = await this.#Address.findAll({
where: where,
limit: limit,
raw: true,
order: ['name', 'cityMunName']
})
} else {
let criteria = this.#expandNameCriteria(keys[0])
addresses = await this.#Address.findAll({
where: {
[Op.or]: criteria,
[Op.and]: [
{
cityMunName: {
[Op.or]: {
[Op.like]: keys[1],
}
}
},
{
provName: {
[Op.or]: {
[Op.like]: keys[2],
[Op.like]: keys[2] + '%',
}
}
}
]
},
limit: limit,
raw: true,
order: ['name', 'cityMunName']
})
}
if (!formatter) {
formatter = (a) => {
let full = []
if (a.name) full.push(a.name)
if (a.cityMunName) full.push(a.cityMunName)
if (a.provName && (a.provName !== a.cityMunName)) full.push(a.provName)
if (a.regName) full.push(a.regName)
return {
name: full.join(', '),
id: a.psgc
}
}
}
return addresses.map(formatter)
} catch (err) {
throw err
}
}
#expandNameCriteria (keyWords) {
// Broader search criteria
let criteria = [
{
name: {
[Op.like]: keyWords + '%',
}
}
]
if (keyWords.match(/^(sto\.?)/i)) {
criteria.push({
name: {
[Op.like]: keyWords.replace(/^(sto\.?)/i, 'Santo') + '%',
}
})
} else if (keyWords.match(/^(brgy\.?)/i)) {
criteria.push({
name: {
[Op.like]: keyWords.replace(/^(brgy\.?)/i, 'Barangay') + '%',
}
})
}
if (keyWords.match(/(nino)/i)) {
criteria.push({
name: {
[Op.like]: keyWords.replace(/(nino)/i, 'niño') + '%',
}
})
}
return criteria
}
}
module.exports = PhAddressFinder