-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentify
More file actions
90 lines (77 loc) · 2.69 KB
/
identify
File metadata and controls
90 lines (77 loc) · 2.69 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const { v4: uuidv4 } = require('uuid');
const moment = require('moment');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(bodyParser.json());
// Database simulation (in-memory for demonstration)
let contacts = [];
// Helper function to find a contact by email or phone number
function findContact(email = null, phoneNumber = null) {
return contacts.find(contact =>
(email && contact.email === email) ||
(phoneNumber && contact.phoneNumber === phoneNumber)
);
}
// Helper function to create a new contact
function createContact(email = null, phoneNumber = null, linkedId = null, linkPrecedence = null) {
const newContact = {
id: uuidv4(),
phoneNumber,
email,
linkedId,
linkPrecedence,
createdAt: moment().toISOString(),
updatedAt: moment().toISOString(),
deletedAt: null
};
contacts.push(newContact);
return newContact;
}
// Endpoint for identifying and consolidating contacts
app.post('/identify', (req, res) => {
const { email, phoneNumber } = req.body;
// Check if there's an existing contact with the provided email or phone number
const existingContact = findContact(email, phoneNumber);
if (existingContact) {
const primaryContactId = existingContact.id;
const emails = [existingContact.email];
const phoneNumbers = [existingContact.phoneNumber];
const secondaryContactIds = [];
// Check if there's any secondary contact linked to the primary contact
contacts.forEach(contact => {
if (contact.linkedId === primaryContactId) {
emails.push(contact.email);
phoneNumbers.push(contact.phoneNumber);
secondaryContactIds.push(contact.id);
}
});
const response = {
contact: {
primaryContactId,
emails,
phoneNumbers,
secondaryContactIds
}
};
return res.status(200).json(response);
} else {
// If no existing contact found, create a new one and return it
const newContact = createContact(email, phoneNumber, null, 'primary');
const response = {
contact: {
primaryContactId: newContact.id,
emails: [newContact.email],
phoneNumbers: [newContact.phoneNumber],
secondaryContactIds: []
}
};
return res.status(200).json(response);
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});