-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
45 lines (40 loc) · 1.3 KB
/
index.ts
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
import * as fs from 'fs-extra';
import * as path from 'path';
import { haversine, spherical } from './distance-algos';
import { convertTxtToJSON, readFileJSON } from './file-ops';
import { parseArgs } from './parse-args';
import { sort } from './utils';
import ICustomer from './Customer';
/**
* Find the customers, as per search criteria and input
* @param customers JSON Array<Object> of customers
* @param findConfig JSON config with proximity and information
* @return Array<Object> who are within in the proximity.
*/
export function find(customers, findConfig) {
const customerListSorted = sort(customers, 'user_id');
const inviteList = customerListSorted.filter((c) => {
// choose between haversine or spherical algo
c.inKm = haversine(
c.latitude, c.longitude,
findConfig.locationCordinates.lat, findConfig.locationCordinates.long,
);
return c.inKm <= findConfig.proximity; // within the proxity.
});
return inviteList;
}
/**
* Main entry of the module.
* @async
*/
export default async function main() {
try {
const args = parseArgs(process.argv);
const config = await readFileJSON(args.config);
const customers = await convertTxtToJSON(args.file);
const inviteList = find(customers, config);
return inviteList;
} catch (error) {
throw error;
}
}