-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactPersonController.js
More file actions
237 lines (237 loc) · 9.79 KB
/
contactPersonController.js
File metadata and controls
237 lines (237 loc) · 9.79 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setPrimaryContact = exports.deleteContactPerson = exports.updateContactPerson = exports.createContactPerson = exports.getContactPersons = void 0;
// Fix the import path to match the working path used in other files
const prismaClient_1 = __importDefault(require("../../utils/prismaClient"));
// Get all contact persons for a customer
const getContactPersons = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { customerId } = req.params;
console.log(`[ContactCtrl] Attempt get contacts for customer ${customerId}`);
// Check if customer exists
const customer = yield prismaClient_1.default.customer.findUnique({
where: { id: customerId },
select: { id: true }
});
if (!customer) {
res.status(404).json({ error: 'Customer not found' });
return;
}
// Get all contacts for this customer
const contacts = yield prismaClient_1.default.contactPerson.findMany({
where: { customerId },
orderBy: [
{ isPrimary: 'desc' },
{ name: 'asc' }
]
});
console.log(`[ContactCtrl] Fetched ${contacts.length} contacts for customer ${customerId}`);
res.json(contacts);
}
catch (error) {
console.error('Error fetching contact persons:', error);
res.status(500).json({ error: 'Failed to fetch contact persons', details: error.message });
}
});
exports.getContactPersons = getContactPersons;
// Create a new contact person
const createContactPerson = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
try {
const { customerId } = req.params;
const { name, email, phone, role, notes, isPrimary } = req.body;
// Validate required fields
if (!name) {
res.status(400).json({ error: 'Name is required' });
return;
}
// Check if customer exists
const customer = yield prismaClient_1.default.customer.findUnique({
where: { id: customerId },
select: { id: true }
});
if (!customer) {
res.status(404).json({ error: 'Customer not found' });
return;
}
// Check for email uniqueness if provided
if (email) {
const existingContact = yield prismaClient_1.default.contactPerson.findUnique({
where: { email }
});
if (existingContact) {
res.status(400).json({ error: 'Email is already associated with another contact' });
return;
}
}
// If this contact is being set as primary, update all other contacts to not be primary
if (isPrimary) {
yield prismaClient_1.default.contactPerson.updateMany({
where: { customerId },
data: { isPrimary: false }
});
}
// Create the contact person
const contact = yield prismaClient_1.default.contactPerson.create({
data: {
name,
email,
phone,
role,
notes,
isPrimary: isPrimary || false,
customer: {
connect: { id: customerId }
}
}
});
res.status(201).json(contact);
}
catch (error) {
console.error('Error creating contact person:', error);
if (error.code === 'P2002' && ((_b = (_a = error.meta) === null || _a === void 0 ? void 0 : _a.target) === null || _b === void 0 ? void 0 : _b.includes('email'))) {
res.status(400).json({ error: 'Email address is already in use. Please use a different email.' });
return;
}
res.status(500).json({ error: 'Failed to create contact person', details: error.message });
}
});
exports.createContactPerson = createContactPerson;
// Update an existing contact person
const updateContactPerson = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
try {
const { customerId, contactId } = req.params;
const { name, email, phone, role, notes, isPrimary } = req.body;
// Validate required fields
if (!name) {
res.status(400).json({ error: 'Name is required' });
return;
}
// Check if contact exists and belongs to the specified customer
const existingContact = yield prismaClient_1.default.contactPerson.findFirst({
where: {
id: contactId,
customerId
}
});
if (!existingContact) {
res.status(404).json({ error: 'Contact not found or does not belong to this customer' });
return;
}
// Check for email uniqueness if email is changing
if (email && email !== existingContact.email) {
const emailInUse = yield prismaClient_1.default.contactPerson.findUnique({
where: { email }
});
if (emailInUse && emailInUse.id !== contactId) {
res.status(400).json({ error: 'Email is already associated with another contact' });
return;
}
}
// If this contact is being set as primary, update all other contacts to not be primary
if (isPrimary && !existingContact.isPrimary) {
yield prismaClient_1.default.contactPerson.updateMany({
where: {
customerId,
id: { not: contactId }
},
data: { isPrimary: false }
});
}
// Update the contact
const updatedContact = yield prismaClient_1.default.contactPerson.update({
where: { id: contactId },
data: {
name,
email,
phone,
role,
notes,
isPrimary: isPrimary || false
}
});
res.json(updatedContact);
}
catch (error) {
console.error('Error updating contact person:', error);
if (error.code === 'P2002' && ((_b = (_a = error.meta) === null || _a === void 0 ? void 0 : _a.target) === null || _b === void 0 ? void 0 : _b.includes('email'))) {
res.status(400).json({ error: 'Email address is already in use. Please use a different email.' });
return;
}
res.status(500).json({ error: 'Failed to update contact person', details: error.message });
}
});
exports.updateContactPerson = updateContactPerson;
// Delete a contact person
const deleteContactPerson = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { customerId, contactId } = req.params;
// Check if contact exists and belongs to the specified customer
const existingContact = yield prismaClient_1.default.contactPerson.findFirst({
where: {
id: contactId,
customerId
}
});
if (!existingContact) {
res.status(404).json({ error: 'Contact not found or does not belong to this customer' });
return;
}
// Delete the contact
yield prismaClient_1.default.contactPerson.delete({
where: { id: contactId }
});
res.json({ message: 'Contact person deleted successfully' });
}
catch (error) {
console.error('Error deleting contact person:', error);
res.status(500).json({ error: 'Failed to delete contact person', details: error.message });
}
});
exports.deleteContactPerson = deleteContactPerson;
// Set a contact as the primary contact for a customer
const setPrimaryContact = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
try {
const { customerId, contactId } = req.params;
// Check if contact exists and belongs to the specified customer
const existingContact = yield prismaClient_1.default.contactPerson.findFirst({
where: {
id: contactId,
customerId
}
});
if (!existingContact) {
res.status(404).json({ error: 'Contact not found or does not belong to this customer' });
return;
}
// Update all contacts for this customer to not be primary
yield prismaClient_1.default.contactPerson.updateMany({
where: { customerId },
data: { isPrimary: false }
});
// Set the specified contact as primary
const updatedContact = yield prismaClient_1.default.contactPerson.update({
where: { id: contactId },
data: { isPrimary: true }
});
res.json(updatedContact);
}
catch (error) {
console.error('Error setting primary contact:', error);
res.status(500).json({ error: 'Failed to set primary contact', details: error.message });
}
});
exports.setPrimaryContact = setPrimaryContact;