@@ -5,18 +5,18 @@ import prisma from '$lib/prisma';
5
5
export async function load ( { params, url } ) {
6
6
try {
7
7
const accountId = params . accountId ;
8
-
8
+
9
9
// Fetch account details
10
10
const account = await prisma . account . findUnique ( {
11
11
where : {
12
12
id : accountId
13
13
}
14
14
} ) ;
15
-
15
+
16
16
if ( ! account ) {
17
17
throw error ( 404 , 'Account not found' ) ;
18
18
}
19
-
19
+
20
20
// Fetch account contacts
21
21
const contactRelationships = await prisma . accountContactRelationship . findMany ( {
22
22
where : {
@@ -26,21 +26,21 @@ export async function load({ params, url }) {
26
26
contact : true
27
27
}
28
28
} ) ;
29
-
29
+
30
30
// Format contacts with isPrimary flag
31
31
const contacts = contactRelationships . map ( rel => ( {
32
32
...rel . contact ,
33
33
isPrimary : rel . isPrimary ,
34
34
role : rel . role
35
35
} ) ) ;
36
-
36
+
37
37
// Fetch account opportunities
38
38
const opportunities = await prisma . opportunity . findMany ( {
39
39
where : {
40
40
accountId : accountId
41
41
}
42
42
} ) ;
43
-
43
+
44
44
// Fetch account comments/notes
45
45
const comments = await prisma . comment . findMany ( {
46
46
where : {
@@ -62,14 +62,14 @@ export async function load({ params, url }) {
62
62
if ( url . searchParams . get ( 'commentsOnly' ) === '1' ) {
63
63
return new Response ( JSON . stringify ( { comments } ) , { headers : { 'Content-Type' : 'application/json' } } ) ;
64
64
}
65
-
65
+
66
66
// Fetch account quotes
67
67
const quotes = await prisma . quote . findMany ( {
68
68
where : {
69
69
accountId : accountId
70
70
}
71
71
} ) ;
72
-
72
+
73
73
// Fetch account tasks
74
74
const tasks = await prisma . task . findMany ( {
75
75
where : {
@@ -84,14 +84,24 @@ export async function load({ params, url }) {
84
84
}
85
85
}
86
86
} ) ;
87
-
87
+
88
88
// Fetch account cases
89
89
const cases = await prisma . case . findMany ( {
90
90
where : {
91
91
accountId : accountId
92
92
}
93
93
} ) ;
94
-
94
+
95
+ // Load users in the same organization for assignment
96
+ const users = await prisma . user . findMany ( {
97
+ where : {
98
+ organizations : {
99
+ some : { organizationId : account . organizationId }
100
+ }
101
+ } ,
102
+ select : { id : true , name : true , email : true }
103
+ } ) ;
104
+
95
105
return {
96
106
account,
97
107
contacts,
@@ -100,6 +110,7 @@ export async function load({ params, url }) {
100
110
quotes,
101
111
tasks,
102
112
cases,
113
+ users,
103
114
meta : {
104
115
title : account . name ,
105
116
description : `Account details for ${ account . name } `
@@ -116,19 +127,19 @@ export const actions = {
116
127
closeAccount : async ( { params, request, locals } ) => {
117
128
try {
118
129
const user = locals . user ;
119
-
130
+
120
131
if ( ! user ) {
121
132
return fail ( 401 , { success : false , message : 'Unauthorized' } ) ;
122
133
}
123
-
134
+
124
135
const { accountId } = params ;
125
136
const formData = await request . formData ( ) ;
126
137
const closureReason = formData . get ( 'closureReason' ) ?. toString ( ) ;
127
-
138
+
128
139
if ( ! closureReason ) {
129
140
return fail ( 400 , { success : false , message : 'Please provide a reason for closing this account' } ) ;
130
141
}
131
-
142
+
132
143
// Fetch the account to verify it exists
133
144
const account = await prisma . account . findUnique ( {
134
145
where : { id : accountId } ,
@@ -139,32 +150,32 @@ export const actions = {
139
150
ownerId : true
140
151
}
141
152
} ) ;
142
-
153
+
143
154
if ( ! account ) {
144
155
return fail ( 404 , { success : false , message : 'Account not found' } ) ;
145
156
}
146
-
157
+
147
158
if ( account . closedAt ) {
148
159
return fail ( 400 , { success : false , message : 'Account is already closed' } ) ;
149
160
}
150
-
161
+
151
162
// Check user permissions (must be the owner, a sales manager, or admin)
152
163
const userOrg = await prisma . userOrganization . findFirst ( {
153
164
where : {
154
165
userId : user . id ,
155
166
organizationId : account . organizationId
156
167
}
157
168
} ) ;
158
-
159
- const hasPermission =
160
- user . id === account . ownerId ||
161
- userOrg ?. role === 'ADMIN' ||
169
+
170
+ const hasPermission =
171
+ user . id === account . ownerId ||
172
+ userOrg ?. role === 'ADMIN' ||
162
173
userOrg ?. role === 'SALES_MANAGER' ;
163
-
174
+
164
175
if ( ! hasPermission ) {
165
176
return fail ( 403 , { success : false , message : 'Permission denied. Only account owners, sales managers, or admins can close accounts.' } ) ;
166
177
}
167
-
178
+
168
179
// Update the account to mark it as closed
169
180
const updatedAccount = await prisma . account . update ( {
170
181
where : { id : accountId } ,
@@ -173,7 +184,7 @@ export const actions = {
173
184
closureReason
174
185
}
175
186
} ) ;
176
-
187
+
177
188
// Log this action in the audit log
178
189
await prisma . auditLog . create ( {
179
190
data : {
@@ -188,24 +199,24 @@ export const actions = {
188
199
ipAddress : request . headers . get ( 'x-forwarded-for' ) || 'unknown'
189
200
}
190
201
} ) ;
191
-
202
+
192
203
return { success : true } ;
193
204
} catch ( error ) {
194
205
console . error ( 'Error closing account:' , error ) ;
195
206
return fail ( 500 , { success : false , message : 'An unexpected error occurred' } ) ;
196
207
}
197
208
} ,
198
-
209
+
199
210
reopenAccount : async ( { params, request, locals } ) => {
200
211
try {
201
212
const user = locals . user ;
202
-
213
+
203
214
if ( ! user ) {
204
215
return fail ( 401 , { success : false , message : 'Unauthorized' } ) ;
205
216
}
206
-
217
+
207
218
const { accountId } = params ;
208
-
219
+
209
220
// Fetch the account to verify it exists
210
221
const account = await prisma . account . findUnique ( {
211
222
where : { id : accountId } ,
@@ -217,38 +228,38 @@ export const actions = {
217
228
ownerId : true
218
229
}
219
230
} ) ;
220
-
231
+
221
232
if ( ! account ) {
222
233
return fail ( 404 , { success : false , message : 'Account not found' } ) ;
223
234
}
224
-
235
+
225
236
if ( ! account . closedAt ) {
226
237
return fail ( 400 , { success : false , message : 'Account is not closed' } ) ;
227
238
}
228
-
239
+
229
240
// Check user permissions (must be the owner, a sales manager, or admin)
230
241
const userOrg = await prisma . userOrganization . findFirst ( {
231
242
where : {
232
243
userId : user . id ,
233
244
organizationId : account . organizationId
234
245
}
235
246
} ) ;
236
-
237
- const hasPermission =
238
- user . id === account . ownerId ||
239
- userOrg ?. role === 'ADMIN' ||
247
+
248
+ const hasPermission =
249
+ user . id === account . ownerId ||
250
+ userOrg ?. role === 'ADMIN' ||
240
251
userOrg ?. role === 'SALES_MANAGER' ;
241
-
252
+
242
253
if ( ! hasPermission ) {
243
254
return fail ( 403 , { success : false , message : 'Permission denied. Only account owners, sales managers, or admins can reopen accounts.' } ) ;
244
255
}
245
-
256
+
246
257
// Save the old values for the audit log
247
258
const oldValues = {
248
259
closedAt : account . closedAt ,
249
260
closureReason : account . closureReason
250
261
} ;
251
-
262
+
252
263
// Update the account to mark it as reopened
253
264
const updatedAccount = await prisma . account . update ( {
254
265
where : { id : accountId } ,
@@ -257,7 +268,7 @@ export const actions = {
257
268
closureReason : null
258
269
}
259
270
} ) ;
260
-
271
+
261
272
// Log this action in the audit log
262
273
await prisma . auditLog . create ( {
263
274
data : {
@@ -272,7 +283,7 @@ export const actions = {
272
283
ipAddress : request . headers . get ( 'x-forwarded-for' ) || 'unknown'
273
284
}
274
285
} ) ;
275
-
286
+
276
287
return { success : true } ;
277
288
} catch ( error ) {
278
289
console . error ( 'Error reopening account:' , error ) ;
@@ -372,7 +383,8 @@ export const actions = {
372
383
}
373
384
} ,
374
385
375
- comment : async ( { request, params } ) => {
386
+ comment : async ( { request, params, locals } ) => {
387
+ const user = locals . user ;
376
388
// Fallback: fetch account to get organizationId
377
389
const account = await prisma . account . findUnique ( {
378
390
where : { id : params . accountId } ,
@@ -396,5 +408,45 @@ export const actions = {
396
408
}
397
409
} ) ;
398
410
return { success : true } ;
411
+ } ,
412
+
413
+ addTask : async ( { params, request, locals } ) => {
414
+ try {
415
+ const user = locals . user ;
416
+ const org = locals . org ;
417
+ if ( ! user || ! org ) {
418
+ return fail ( 401 , { success : false , message : 'Unauthorized' } ) ;
419
+ }
420
+ const { accountId } = params ;
421
+ const formData = await request . formData ( ) ;
422
+ const subject = formData . get ( 'subject' ) ?. toString ( ) . trim ( ) ;
423
+ const description = formData . get ( 'description' ) ?. toString ( ) || '' ;
424
+ const dueDateRaw = formData . get ( 'dueDate' ) ;
425
+ const dueDate = dueDateRaw ? new Date ( dueDateRaw . toString ( ) ) : null ;
426
+ const priority = formData . get ( 'priority' ) ?. toString ( ) || 'Normal' ;
427
+ if ( ! subject ) {
428
+ return fail ( 400 , { success : false , message : 'Subject is required.' } ) ;
429
+ }
430
+ // If no ownerId is provided, default to current user
431
+ // if (!ownerId) ownerId = user.id;
432
+ console . log ( user . id , org . id ) ;
433
+ const task = await prisma . task . create ( {
434
+ data : {
435
+ subject,
436
+ description,
437
+ dueDate,
438
+ priority,
439
+ status : 'Open' ,
440
+ createdBy : { connect : { id : user . id } } ,
441
+ account : { connect : { id : accountId } } ,
442
+ owner : { connect : { id : user . id } } ,
443
+ organization : { connect : { id : org . id } } ,
444
+ }
445
+ } ) ;
446
+ return { success : true , message : 'Task added successfully.' , task } ;
447
+ } catch ( err ) {
448
+ console . error ( 'Error adding task:' , err ) ;
449
+ return fail ( 500 , { success : false , message : 'Failed to add task.' } ) ;
450
+ }
399
451
}
400
452
} ;
0 commit comments