@primocaredentgroup/elettromedicali 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.d.ts +72 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +233 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +94 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +1444 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/apiKeys.d.ts +69 -0
- package/dist/component/apiKeys.d.ts.map +1 -0
- package/dist/component/apiKeys.js +207 -0
- package/dist/component/apiKeys.js.map +1 -0
- package/dist/component/clinics.d.ts +103 -0
- package/dist/component/clinics.d.ts.map +1 -0
- package/dist/component/clinics.js +126 -0
- package/dist/component/clinics.js.map +1 -0
- package/dist/component/contracts.d.ts +85 -0
- package/dist/component/contracts.d.ts.map +1 -0
- package/dist/component/contracts.js +115 -0
- package/dist/component/contracts.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +3 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +3 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +7 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/dashboardStats.d.ts +14 -0
- package/dist/component/dashboardStats.d.ts.map +1 -0
- package/dist/component/dashboardStats.js +136 -0
- package/dist/component/dashboardStats.js.map +1 -0
- package/dist/component/dashboardStatsCache.d.ts +32 -0
- package/dist/component/dashboardStatsCache.d.ts.map +1 -0
- package/dist/component/dashboardStatsCache.js +129 -0
- package/dist/component/dashboardStatsCache.js.map +1 -0
- package/dist/component/deviceCategories.d.ts +108 -0
- package/dist/component/deviceCategories.d.ts.map +1 -0
- package/dist/component/deviceCategories.js +254 -0
- package/dist/component/deviceCategories.js.map +1 -0
- package/dist/component/deviceQuestions.d.ts +129 -0
- package/dist/component/deviceQuestions.d.ts.map +1 -0
- package/dist/component/deviceQuestions.js +175 -0
- package/dist/component/deviceQuestions.js.map +1 -0
- package/dist/component/deviceRepairHistory.d.ts +30 -0
- package/dist/component/deviceRepairHistory.d.ts.map +1 -0
- package/dist/component/deviceRepairHistory.js +84 -0
- package/dist/component/deviceRepairHistory.js.map +1 -0
- package/dist/component/deviceStatus.d.ts +63 -0
- package/dist/component/deviceStatus.d.ts.map +1 -0
- package/dist/component/deviceStatus.js +58 -0
- package/dist/component/deviceStatus.js.map +1 -0
- package/dist/component/devices.d.ts +299 -0
- package/dist/component/devices.d.ts.map +1 -0
- package/dist/component/devices.js +587 -0
- package/dist/component/devices.js.map +1 -0
- package/dist/component/emailHelpers.d.ts +17 -0
- package/dist/component/emailHelpers.d.ts.map +1 -0
- package/dist/component/emailHelpers.js +39 -0
- package/dist/component/emailHelpers.js.map +1 -0
- package/dist/component/emails.d.ts +56 -0
- package/dist/component/emails.d.ts.map +1 -0
- package/dist/component/emails.js +58 -0
- package/dist/component/emails.js.map +1 -0
- package/dist/component/http.d.ts +3 -0
- package/dist/component/http.d.ts.map +1 -0
- package/dist/component/http.js +229 -0
- package/dist/component/http.js.map +1 -0
- package/dist/component/maintenanceTasks.d.ts +733 -0
- package/dist/component/maintenanceTasks.d.ts.map +1 -0
- package/dist/component/maintenanceTasks.js +937 -0
- package/dist/component/maintenanceTasks.js.map +1 -0
- package/dist/component/roles.d.ts +75 -0
- package/dist/component/roles.d.ts.map +1 -0
- package/dist/component/roles.js +98 -0
- package/dist/component/roles.js.map +1 -0
- package/dist/component/schema.d.ts +1295 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +724 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/slaMonitoring.d.ts +32 -0
- package/dist/component/slaMonitoring.d.ts.map +1 -0
- package/dist/component/slaMonitoring.js +111 -0
- package/dist/component/slaMonitoring.js.map +1 -0
- package/dist/component/slaRules.d.ts +72 -0
- package/dist/component/slaRules.d.ts.map +1 -0
- package/dist/component/slaRules.js +193 -0
- package/dist/component/slaRules.js.map +1 -0
- package/dist/component/sparePartOrders.d.ts +177 -0
- package/dist/component/sparePartOrders.d.ts.map +1 -0
- package/dist/component/sparePartOrders.js +243 -0
- package/dist/component/sparePartOrders.js.map +1 -0
- package/dist/component/spareParts.d.ts +472 -0
- package/dist/component/spareParts.d.ts.map +1 -0
- package/dist/component/spareParts.js +319 -0
- package/dist/component/spareParts.js.map +1 -0
- package/dist/component/supplierCategories.d.ts +22 -0
- package/dist/component/supplierCategories.d.ts.map +1 -0
- package/dist/component/supplierCategories.js +64 -0
- package/dist/component/supplierCategories.js.map +1 -0
- package/dist/component/suppliers.d.ts +94 -0
- package/dist/component/suppliers.d.ts.map +1 -0
- package/dist/component/suppliers.js +195 -0
- package/dist/component/suppliers.js.map +1 -0
- package/dist/component/ticketComments.d.ts +89 -0
- package/dist/component/ticketComments.d.ts.map +1 -0
- package/dist/component/ticketComments.js +246 -0
- package/dist/component/ticketComments.js.map +1 -0
- package/dist/component/ticketCustomFields.d.ts +149 -0
- package/dist/component/ticketCustomFields.d.ts.map +1 -0
- package/dist/component/ticketCustomFields.js +215 -0
- package/dist/component/ticketCustomFields.js.map +1 -0
- package/dist/component/ticketExport.d.ts +83 -0
- package/dist/component/ticketExport.d.ts.map +1 -0
- package/dist/component/ticketExport.js +182 -0
- package/dist/component/ticketExport.js.map +1 -0
- package/dist/component/ticketHistory.d.ts +57 -0
- package/dist/component/ticketHistory.d.ts.map +1 -0
- package/dist/component/ticketHistory.js +81 -0
- package/dist/component/ticketHistory.js.map +1 -0
- package/dist/component/ticketMacros.d.ts +141 -0
- package/dist/component/ticketMacros.d.ts.map +1 -0
- package/dist/component/ticketMacros.js +255 -0
- package/dist/component/ticketMacros.js.map +1 -0
- package/dist/component/ticketStatuses.d.ts +60 -0
- package/dist/component/ticketStatuses.d.ts.map +1 -0
- package/dist/component/ticketStatuses.js +110 -0
- package/dist/component/ticketStatuses.js.map +1 -0
- package/dist/component/ticketTriggers.d.ts +408 -0
- package/dist/component/ticketTriggers.d.ts.map +1 -0
- package/dist/component/ticketTriggers.js +941 -0
- package/dist/component/ticketTriggers.js.map +1 -0
- package/dist/component/userProfiles.d.ts +259 -0
- package/dist/component/userProfiles.d.ts.map +1 -0
- package/dist/component/userProfiles.js +634 -0
- package/dist/component/userProfiles.js.map +1 -0
- package/dist/component/vendorArticles.d.ts +64 -0
- package/dist/component/vendorArticles.d.ts.map +1 -0
- package/dist/component/vendorArticles.js +116 -0
- package/dist/component/vendorArticles.js.map +1 -0
- package/dist/test.d.ts +1302 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +7 -0
- package/dist/test.js.map +1 -0
- package/package.json +71 -0
- package/src/client/index.ts +344 -0
- package/src/component/_generated/api.ts +110 -0
- package/src/component/_generated/component.ts +2460 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/apiKeys.ts +229 -0
- package/src/component/clinics.ts +136 -0
- package/src/component/contracts.ts +136 -0
- package/src/component/convex.config.js +2 -0
- package/src/component/convex.config.ts +3 -0
- package/src/component/crons.ts +18 -0
- package/src/component/dashboardStats.ts +141 -0
- package/src/component/dashboardStatsCache.ts +145 -0
- package/src/component/deviceCategories.ts +280 -0
- package/src/component/deviceQuestions.ts +225 -0
- package/src/component/deviceRepairHistory.ts +94 -0
- package/src/component/deviceStatus.ts +79 -0
- package/src/component/devices.ts +645 -0
- package/src/component/emailHelpers.ts +38 -0
- package/src/component/emails.ts +61 -0
- package/src/component/http.ts +231 -0
- package/src/component/maintenanceTasks.ts +1003 -0
- package/src/component/roles.ts +99 -0
- package/src/component/schema.ts +842 -0
- package/src/component/slaMonitoring.ts +125 -0
- package/src/component/slaRules.ts +231 -0
- package/src/component/sparePartOrders.ts +290 -0
- package/src/component/spareParts.ts +362 -0
- package/src/component/supplierCategories.ts +65 -0
- package/src/component/suppliers.ts +234 -0
- package/src/component/ticketComments.ts +288 -0
- package/src/component/ticketCustomFields.ts +260 -0
- package/src/component/ticketExport.ts +220 -0
- package/src/component/ticketHistory.ts +106 -0
- package/src/component/ticketMacros.ts +291 -0
- package/src/component/ticketStatuses.ts +109 -0
- package/src/component/ticketTriggers.ts +1152 -0
- package/src/component/userProfiles.ts +745 -0
- package/src/component/vendorArticles.ts +139 -0
- package/src/test.ts +15 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { query } from './_generated/server';
|
|
3
|
+
|
|
4
|
+
export const exportTickets = query({
|
|
5
|
+
args: {
|
|
6
|
+
statuses: v.optional(v.array(v.string())),
|
|
7
|
+
priorities: v.optional(v.array(v.string())),
|
|
8
|
+
clinicIds: v.optional(v.array(v.id('clinics'))),
|
|
9
|
+
supplierIds: v.optional(v.array(v.id('suppliers'))),
|
|
10
|
+
createdByEmail: v.optional(v.string()),
|
|
11
|
+
ticketNumber: v.optional(v.string()),
|
|
12
|
+
dateFrom: v.optional(v.number()),
|
|
13
|
+
dateTo: v.optional(v.number()),
|
|
14
|
+
slaBreached: v.optional(v.boolean()),
|
|
15
|
+
isExternal: v.optional(v.boolean()),
|
|
16
|
+
needsAssignment: v.optional(v.boolean()),
|
|
17
|
+
hasDevice: v.optional(v.boolean()),
|
|
18
|
+
deviceCategory: v.optional(v.string()),
|
|
19
|
+
deviceBrand: v.optional(v.string()),
|
|
20
|
+
},
|
|
21
|
+
handler: async (ctx, args) => {
|
|
22
|
+
let tickets = await ctx.db.query('maintenance_tasks').order('desc').take(1500);
|
|
23
|
+
|
|
24
|
+
if (args.statuses && args.statuses.length > 0) {
|
|
25
|
+
tickets = tickets.filter(t => args.statuses!.includes(t.status));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (args.priorities && args.priorities.length > 0) {
|
|
29
|
+
tickets = tickets.filter(t => t.priority && args.priorities!.includes(t.priority));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (args.clinicIds && args.clinicIds.length > 0) {
|
|
33
|
+
tickets = tickets.filter(t => args.clinicIds!.includes(t.clinicId));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (args.supplierIds && args.supplierIds.length > 0) {
|
|
37
|
+
tickets = tickets.filter(t => t.supplierId && args.supplierIds!.includes(t.supplierId));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (args.createdByEmail) {
|
|
41
|
+
const emailLower = args.createdByEmail.toLowerCase();
|
|
42
|
+
tickets = tickets.filter(t =>
|
|
43
|
+
t.createdByEmail?.toLowerCase().includes(emailLower)
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (args.ticketNumber) {
|
|
48
|
+
const numLower = args.ticketNumber.toLowerCase();
|
|
49
|
+
tickets = tickets.filter(t =>
|
|
50
|
+
t.ticketNumber?.toLowerCase().includes(numLower)
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (args.dateFrom) {
|
|
55
|
+
tickets = tickets.filter(t => t.created_at >= args.dateFrom!);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (args.dateTo) {
|
|
59
|
+
tickets = tickets.filter(t => t.created_at <= args.dateTo!);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (args.slaBreached !== undefined) {
|
|
63
|
+
tickets = tickets.filter(t => t.slaBreached === args.slaBreached);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (args.isExternal !== undefined) {
|
|
67
|
+
tickets = tickets.filter(t => t.isExternal === args.isExternal);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (args.needsAssignment !== undefined) {
|
|
71
|
+
tickets = tickets.filter(t => t.needsAssignment === args.needsAssignment);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (args.hasDevice !== undefined) {
|
|
75
|
+
if (args.hasDevice) {
|
|
76
|
+
tickets = tickets.filter(t => t.deviceId);
|
|
77
|
+
} else {
|
|
78
|
+
tickets = tickets.filter(t => !t.deviceId);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const clinicIds = [...new Set(tickets.map(t => t.clinicId))].slice(0, 300);
|
|
83
|
+
const supplierIds = [...new Set(tickets.filter(t => t.supplierId).map(t => t.supplierId!))].slice(0, 300);
|
|
84
|
+
const deviceIds = [...new Set(tickets.filter(t => t.deviceId).map(t => t.deviceId!))].slice(0, 500);
|
|
85
|
+
|
|
86
|
+
const clinicsMap = new Map();
|
|
87
|
+
for (const id of clinicIds) {
|
|
88
|
+
const clinic = await ctx.db.get(id);
|
|
89
|
+
if (clinic) clinicsMap.set(id, clinic);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const suppliersMap = new Map();
|
|
93
|
+
for (const id of supplierIds) {
|
|
94
|
+
const supplier = await ctx.db.get(id);
|
|
95
|
+
if (supplier) suppliersMap.set(id, supplier);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const devicesMap = new Map();
|
|
99
|
+
for (const id of deviceIds) {
|
|
100
|
+
const device = await ctx.db.get(id);
|
|
101
|
+
if (device) devicesMap.set(id, device);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (args.deviceCategory || args.deviceBrand) {
|
|
105
|
+
tickets = tickets.filter(t => {
|
|
106
|
+
if (!t.deviceId) return false;
|
|
107
|
+
const device = devicesMap.get(t.deviceId);
|
|
108
|
+
if (!device) return false;
|
|
109
|
+
|
|
110
|
+
if (args.deviceCategory && device.category !== args.deviceCategory) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if (args.deviceBrand && device.brand !== args.deviceBrand) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const statuses = await ctx.db.query('ticket_statuses').collect();
|
|
121
|
+
const statusMap = new Map(statuses.map(s => [s.name, s.label]));
|
|
122
|
+
|
|
123
|
+
const enrichedTickets = tickets.map(t => {
|
|
124
|
+
const clinic = clinicsMap.get(t.clinicId);
|
|
125
|
+
const supplier = t.supplierId ? suppliersMap.get(t.supplierId) : null;
|
|
126
|
+
const device = t.deviceId ? devicesMap.get(t.deviceId) : null;
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
_id: t._id,
|
|
130
|
+
ticketNumber: t.ticketNumber || '-',
|
|
131
|
+
|
|
132
|
+
title: t.title || '-',
|
|
133
|
+
description: t.description,
|
|
134
|
+
status: t.status,
|
|
135
|
+
statusLabel: statusMap.get(t.status) || t.status,
|
|
136
|
+
priority: t.priority || '-',
|
|
137
|
+
|
|
138
|
+
createdByEmail: t.createdByEmail || '-',
|
|
139
|
+
created_by: t.created_by,
|
|
140
|
+
|
|
141
|
+
created_at: t.created_at,
|
|
142
|
+
createdAtFormatted: new Date(t.created_at).toLocaleString('it-IT'),
|
|
143
|
+
updated_at: t.updated_at,
|
|
144
|
+
updatedAtFormatted: new Date(t.updated_at).toLocaleString('it-IT'),
|
|
145
|
+
|
|
146
|
+
clinicId: t.clinicId,
|
|
147
|
+
clinicName: clinic?.name || '-',
|
|
148
|
+
clinicAddress: clinic?.address || '-',
|
|
149
|
+
clinicRegion: clinic?.region || '-',
|
|
150
|
+
|
|
151
|
+
supplierId: t.supplierId || null,
|
|
152
|
+
supplierName: supplier?.name || '-',
|
|
153
|
+
supplierEmail: supplier?.contact_email || '-',
|
|
154
|
+
|
|
155
|
+
deviceId: t.deviceId || null,
|
|
156
|
+
deviceName: device?.name || '-',
|
|
157
|
+
deviceCategory: device?.category || '-',
|
|
158
|
+
deviceBrand: device?.brand || '-',
|
|
159
|
+
deviceModel: device?.model || '-',
|
|
160
|
+
deviceSerialNumber: device?.serial_number || '-',
|
|
161
|
+
|
|
162
|
+
slaDeadline: t.slaDeadline || null,
|
|
163
|
+
slaDeadlineFormatted: t.slaDeadline ? new Date(t.slaDeadline).toLocaleString('it-IT') : '-',
|
|
164
|
+
slaBreached: t.slaBreached || false,
|
|
165
|
+
|
|
166
|
+
isExternal: t.isExternal || false,
|
|
167
|
+
needsAssignment: t.needsAssignment || false,
|
|
168
|
+
|
|
169
|
+
notes: t.notes || '-',
|
|
170
|
+
customFields: t.customFields || {},
|
|
171
|
+
|
|
172
|
+
externalTicketId: t.externalTicketId || '-',
|
|
173
|
+
externalTicketNumber: t.externalTicketNumber || '-',
|
|
174
|
+
importSource: t.importSource || '-',
|
|
175
|
+
|
|
176
|
+
photosCount: t.photos?.length || 0,
|
|
177
|
+
|
|
178
|
+
deviceQuestionAnswers: t.deviceQuestionAnswers || [],
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
enrichedTickets.sort((a, b) => b.created_at - a.created_at);
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
tickets: enrichedTickets,
|
|
186
|
+
totalCount: enrichedTickets.length,
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
export const getFilterOptions = query({
|
|
192
|
+
args: {},
|
|
193
|
+
handler: async (ctx) => {
|
|
194
|
+
const statuses = await ctx.db.query('ticket_statuses').collect();
|
|
195
|
+
const statusOptions = statuses.map(s => ({ value: s.name, label: s.label }));
|
|
196
|
+
|
|
197
|
+
const clinics = await ctx.db.query('clinics').collect();
|
|
198
|
+
const clinicOptions = clinics.map(c => ({ value: c._id, label: c.name }));
|
|
199
|
+
|
|
200
|
+
const suppliers = await ctx.db.query('suppliers').collect();
|
|
201
|
+
const supplierOptions = suppliers.map(s => ({ value: s._id, label: s.name }));
|
|
202
|
+
|
|
203
|
+
const deviceSample = await ctx.db.query('devices').take(2000);
|
|
204
|
+
const categories = [...new Set(deviceSample.map(d => d.category).filter(Boolean))];
|
|
205
|
+
const brands = [...new Set(deviceSample.map(d => d.brand).filter(Boolean))];
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
statuses: statusOptions,
|
|
209
|
+
priorities: [
|
|
210
|
+
{ value: 'low', label: 'Bassa' },
|
|
211
|
+
{ value: 'medium', label: 'Media' },
|
|
212
|
+
{ value: 'high', label: 'Alta' },
|
|
213
|
+
],
|
|
214
|
+
clinics: clinicOptions.sort((a, b) => a.label.localeCompare(b.label)),
|
|
215
|
+
suppliers: supplierOptions.sort((a, b) => a.label.localeCompare(b.label)),
|
|
216
|
+
deviceCategories: categories.sort(),
|
|
217
|
+
deviceBrands: brands.sort(),
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { query, mutation, internalMutation } from './_generated/server';
|
|
3
|
+
|
|
4
|
+
// Get ticket history (admin only)
|
|
5
|
+
export const getTicketHistory = query({
|
|
6
|
+
args: { ticketId: v.id('maintenance_tasks') },
|
|
7
|
+
handler: async (ctx, args) => {
|
|
8
|
+
const history = await ctx.db
|
|
9
|
+
.query('ticket_history')
|
|
10
|
+
.withIndex('by_ticketId', (q: any) => q.eq('ticketId', args.ticketId))
|
|
11
|
+
.take(200);
|
|
12
|
+
|
|
13
|
+
// Sort by timestamp descending (most recent first)
|
|
14
|
+
return history.sort((a, b) => b.timestamp - a.timestamp);
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// Add history entry (internal mutation - called by other functions)
|
|
19
|
+
export const addHistoryEntry = internalMutation({
|
|
20
|
+
args: {
|
|
21
|
+
ticketId: v.id('maintenance_tasks'),
|
|
22
|
+
eventType: v.union(
|
|
23
|
+
v.literal('created'),
|
|
24
|
+
v.literal('status_change'),
|
|
25
|
+
v.literal('assignee_change'),
|
|
26
|
+
v.literal('priority_change'),
|
|
27
|
+
v.literal('trigger_executed'),
|
|
28
|
+
v.literal('comment_added'),
|
|
29
|
+
v.literal('sla_set'),
|
|
30
|
+
v.literal('sla_warning'),
|
|
31
|
+
v.literal('sla_breach'),
|
|
32
|
+
v.literal('updated')
|
|
33
|
+
),
|
|
34
|
+
oldValue: v.optional(v.string()),
|
|
35
|
+
newValue: v.optional(v.string()),
|
|
36
|
+
triggerName: v.optional(v.string()),
|
|
37
|
+
triggerId: v.optional(v.id('ticket_triggers')),
|
|
38
|
+
oldSupplierId: v.optional(v.id('suppliers')),
|
|
39
|
+
newSupplierId: v.optional(v.id('suppliers')),
|
|
40
|
+
oldSupplierName: v.optional(v.string()),
|
|
41
|
+
newSupplierName: v.optional(v.string()),
|
|
42
|
+
performedBy: v.optional(v.string()),
|
|
43
|
+
performedByName: v.optional(v.string()),
|
|
44
|
+
performedByEmail: v.optional(v.string()),
|
|
45
|
+
isSystemAction: v.boolean(),
|
|
46
|
+
notes: v.optional(v.string()),
|
|
47
|
+
},
|
|
48
|
+
handler: async (ctx, args) => {
|
|
49
|
+
await ctx.db.insert('ticket_history', {
|
|
50
|
+
...args,
|
|
51
|
+
timestamp: Date.now(),
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Public mutation to add history (for use from frontend/other mutations)
|
|
57
|
+
export const recordHistoryEvent = mutation({
|
|
58
|
+
args: {
|
|
59
|
+
ticketId: v.id('maintenance_tasks'),
|
|
60
|
+
eventType: v.union(
|
|
61
|
+
v.literal('created'),
|
|
62
|
+
v.literal('status_change'),
|
|
63
|
+
v.literal('assignee_change'),
|
|
64
|
+
v.literal('priority_change'),
|
|
65
|
+
v.literal('trigger_executed'),
|
|
66
|
+
v.literal('comment_added'),
|
|
67
|
+
v.literal('sla_set'),
|
|
68
|
+
v.literal('sla_warning'),
|
|
69
|
+
v.literal('sla_breach'),
|
|
70
|
+
v.literal('updated')
|
|
71
|
+
),
|
|
72
|
+
oldValue: v.optional(v.string()),
|
|
73
|
+
newValue: v.optional(v.string()),
|
|
74
|
+
triggerName: v.optional(v.string()),
|
|
75
|
+
triggerId: v.optional(v.id('ticket_triggers')),
|
|
76
|
+
oldSupplierId: v.optional(v.id('suppliers')),
|
|
77
|
+
newSupplierId: v.optional(v.id('suppliers')),
|
|
78
|
+
oldSupplierName: v.optional(v.string()),
|
|
79
|
+
newSupplierName: v.optional(v.string()),
|
|
80
|
+
notes: v.optional(v.string()),
|
|
81
|
+
isSystemAction: v.optional(v.boolean()),
|
|
82
|
+
performedBy: v.optional(v.string()),
|
|
83
|
+
performedByName: v.optional(v.string()),
|
|
84
|
+
performedByEmail: v.optional(v.string()),
|
|
85
|
+
},
|
|
86
|
+
handler: async (ctx, args) => {
|
|
87
|
+
await ctx.db.insert('ticket_history', {
|
|
88
|
+
ticketId: args.ticketId,
|
|
89
|
+
eventType: args.eventType,
|
|
90
|
+
oldValue: args.oldValue,
|
|
91
|
+
newValue: args.newValue,
|
|
92
|
+
triggerName: args.triggerName,
|
|
93
|
+
triggerId: args.triggerId,
|
|
94
|
+
oldSupplierId: args.oldSupplierId,
|
|
95
|
+
newSupplierId: args.newSupplierId,
|
|
96
|
+
oldSupplierName: args.oldSupplierName,
|
|
97
|
+
newSupplierName: args.newSupplierName,
|
|
98
|
+
performedBy: args.performedBy,
|
|
99
|
+
performedByName: args.performedByName,
|
|
100
|
+
performedByEmail: args.performedByEmail,
|
|
101
|
+
isSystemAction: args.isSystemAction ?? false,
|
|
102
|
+
notes: args.notes,
|
|
103
|
+
timestamp: Date.now(),
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { v } from 'convex/values';
|
|
2
|
+
import { query, mutation } from './_generated/server';
|
|
3
|
+
import type { Id } from './_generated/dataModel';
|
|
4
|
+
|
|
5
|
+
export const listMacros = query({
|
|
6
|
+
args: {
|
|
7
|
+
userProfileId: v.optional(v.id('user_profiles')),
|
|
8
|
+
},
|
|
9
|
+
handler: async (ctx, args) => {
|
|
10
|
+
const allMacros = await ctx.db.query('ticket_macros').collect();
|
|
11
|
+
|
|
12
|
+
const filteredMacros = allMacros.filter(m =>
|
|
13
|
+
m.isGlobal || (args.userProfileId && m.createdBy === args.userProfileId)
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const macrosWithCreator = await Promise.all(
|
|
17
|
+
filteredMacros.map(async (macro) => {
|
|
18
|
+
const creator = await ctx.db.get(macro.createdBy);
|
|
19
|
+
return {
|
|
20
|
+
...macro,
|
|
21
|
+
creatorName: creator?.name || creator?.email || 'Sconosciuto',
|
|
22
|
+
creatorEmail: creator?.email,
|
|
23
|
+
isOwner: args.userProfileId ? macro.createdBy === args.userProfileId : false,
|
|
24
|
+
};
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return macrosWithCreator;
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const listActiveMacros = query({
|
|
33
|
+
args: {
|
|
34
|
+
userProfileId: v.optional(v.id('user_profiles')),
|
|
35
|
+
},
|
|
36
|
+
handler: async (ctx, args) => {
|
|
37
|
+
const allMacros = await ctx.db
|
|
38
|
+
.query('ticket_macros')
|
|
39
|
+
.withIndex('by_isActive', (q) => q.eq('isActive', true))
|
|
40
|
+
.collect();
|
|
41
|
+
|
|
42
|
+
return allMacros.filter(m =>
|
|
43
|
+
m.isGlobal || (args.userProfileId && m.createdBy === args.userProfileId)
|
|
44
|
+
).map(m => ({
|
|
45
|
+
_id: m._id,
|
|
46
|
+
name: m.name,
|
|
47
|
+
description: m.description,
|
|
48
|
+
isGlobal: m.isGlobal,
|
|
49
|
+
}));
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const getMacro = query({
|
|
54
|
+
args: {
|
|
55
|
+
macroId: v.id('ticket_macros'),
|
|
56
|
+
userProfileId: v.optional(v.id('user_profiles')),
|
|
57
|
+
},
|
|
58
|
+
handler: async (ctx, args) => {
|
|
59
|
+
const macro = await ctx.db.get(args.macroId);
|
|
60
|
+
if (!macro) {
|
|
61
|
+
throw new Error('Macro non trovata');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!macro.isGlobal && args.userProfileId && macro.createdBy !== args.userProfileId) {
|
|
65
|
+
throw new Error('Non hai accesso a questa macro');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return macro;
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export const createMacro = mutation({
|
|
73
|
+
args: {
|
|
74
|
+
name: v.string(),
|
|
75
|
+
description: v.optional(v.string()),
|
|
76
|
+
isGlobal: v.boolean(),
|
|
77
|
+
actions: v.object({
|
|
78
|
+
changeStatus: v.optional(v.string()),
|
|
79
|
+
assignSupplier: v.optional(v.id('suppliers')),
|
|
80
|
+
assignUser: v.optional(v.id('user_profiles')),
|
|
81
|
+
setSlaHours: v.optional(v.number()),
|
|
82
|
+
applySlaRule: v.optional(v.boolean()),
|
|
83
|
+
addNote: v.optional(v.string()),
|
|
84
|
+
sendNotification: v.optional(v.object({
|
|
85
|
+
recipients: v.array(v.any()),
|
|
86
|
+
message: v.string(),
|
|
87
|
+
})),
|
|
88
|
+
setPriority: v.optional(v.string()),
|
|
89
|
+
}),
|
|
90
|
+
createdBy: v.id('user_profiles'),
|
|
91
|
+
},
|
|
92
|
+
handler: async (ctx, args) => {
|
|
93
|
+
const macroId = await ctx.db.insert('ticket_macros', {
|
|
94
|
+
name: args.name,
|
|
95
|
+
description: args.description,
|
|
96
|
+
isGlobal: args.isGlobal,
|
|
97
|
+
isActive: true,
|
|
98
|
+
actions: args.actions,
|
|
99
|
+
createdAt: Date.now(),
|
|
100
|
+
updatedAt: Date.now(),
|
|
101
|
+
createdBy: args.createdBy,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return macroId;
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
export const updateMacro = mutation({
|
|
109
|
+
args: {
|
|
110
|
+
macroId: v.id('ticket_macros'),
|
|
111
|
+
name: v.optional(v.string()),
|
|
112
|
+
description: v.optional(v.string()),
|
|
113
|
+
isGlobal: v.optional(v.boolean()),
|
|
114
|
+
isActive: v.optional(v.boolean()),
|
|
115
|
+
actions: v.optional(v.object({
|
|
116
|
+
changeStatus: v.optional(v.string()),
|
|
117
|
+
assignSupplier: v.optional(v.id('suppliers')),
|
|
118
|
+
assignUser: v.optional(v.id('user_profiles')),
|
|
119
|
+
setSlaHours: v.optional(v.number()),
|
|
120
|
+
applySlaRule: v.optional(v.boolean()),
|
|
121
|
+
addNote: v.optional(v.string()),
|
|
122
|
+
sendNotification: v.optional(v.object({
|
|
123
|
+
recipients: v.array(v.any()),
|
|
124
|
+
message: v.string(),
|
|
125
|
+
})),
|
|
126
|
+
setPriority: v.optional(v.string()),
|
|
127
|
+
})),
|
|
128
|
+
},
|
|
129
|
+
handler: async (ctx, args) => {
|
|
130
|
+
const macro = await ctx.db.get(args.macroId);
|
|
131
|
+
if (!macro) {
|
|
132
|
+
throw new Error('Macro non trovata');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const updateData: any = { updatedAt: Date.now() };
|
|
136
|
+
if (args.name !== undefined) updateData.name = args.name;
|
|
137
|
+
if (args.description !== undefined) updateData.description = args.description;
|
|
138
|
+
if (args.isGlobal !== undefined) updateData.isGlobal = args.isGlobal;
|
|
139
|
+
if (args.isActive !== undefined) updateData.isActive = args.isActive;
|
|
140
|
+
if (args.actions !== undefined) updateData.actions = args.actions;
|
|
141
|
+
|
|
142
|
+
await ctx.db.patch(args.macroId, updateData);
|
|
143
|
+
return args.macroId;
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
export const deleteMacro = mutation({
|
|
148
|
+
args: { macroId: v.id('ticket_macros') },
|
|
149
|
+
handler: async (ctx, args) => {
|
|
150
|
+
const macro = await ctx.db.get(args.macroId);
|
|
151
|
+
if (!macro) {
|
|
152
|
+
throw new Error('Macro non trovata');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
await ctx.db.delete(args.macroId);
|
|
156
|
+
return { success: true };
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
export const toggleMacro = mutation({
|
|
161
|
+
args: {
|
|
162
|
+
macroId: v.id('ticket_macros'),
|
|
163
|
+
isActive: v.boolean(),
|
|
164
|
+
},
|
|
165
|
+
handler: async (ctx, args) => {
|
|
166
|
+
const macro = await ctx.db.get(args.macroId);
|
|
167
|
+
if (!macro) {
|
|
168
|
+
throw new Error('Macro non trovata');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
await ctx.db.patch(args.macroId, {
|
|
172
|
+
isActive: args.isActive,
|
|
173
|
+
updatedAt: Date.now(),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
return { success: true };
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
export const duplicateMacro = mutation({
|
|
181
|
+
args: {
|
|
182
|
+
macroId: v.id('ticket_macros'),
|
|
183
|
+
newName: v.string(),
|
|
184
|
+
createdBy: v.id('user_profiles'),
|
|
185
|
+
},
|
|
186
|
+
handler: async (ctx, args) => {
|
|
187
|
+
const macro = await ctx.db.get(args.macroId);
|
|
188
|
+
if (!macro) {
|
|
189
|
+
throw new Error('Macro non trovata');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const newMacroId = await ctx.db.insert('ticket_macros', {
|
|
193
|
+
name: args.newName,
|
|
194
|
+
description: macro.description,
|
|
195
|
+
isGlobal: false,
|
|
196
|
+
isActive: false,
|
|
197
|
+
actions: macro.actions,
|
|
198
|
+
createdAt: Date.now(),
|
|
199
|
+
updatedAt: Date.now(),
|
|
200
|
+
createdBy: args.createdBy,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
return newMacroId;
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
export const executeMacro = mutation({
|
|
208
|
+
args: {
|
|
209
|
+
macroId: v.id('ticket_macros'),
|
|
210
|
+
ticketId: v.id('maintenance_tasks'),
|
|
211
|
+
userProfileId: v.optional(v.id('user_profiles')),
|
|
212
|
+
userAuth0Id: v.optional(v.string()),
|
|
213
|
+
userName: v.optional(v.string()),
|
|
214
|
+
userEmail: v.optional(v.string()),
|
|
215
|
+
},
|
|
216
|
+
handler: async (ctx, args) => {
|
|
217
|
+
const macro = await ctx.db.get(args.macroId);
|
|
218
|
+
if (!macro) {
|
|
219
|
+
throw new Error('Macro non trovata');
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (!macro.isActive) {
|
|
223
|
+
throw new Error('Questa macro non è attiva');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (!macro.isGlobal && args.userProfileId && macro.createdBy !== args.userProfileId) {
|
|
227
|
+
throw new Error('Non hai accesso a questa macro');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const ticket = await ctx.db.get(args.ticketId);
|
|
231
|
+
if (!ticket) {
|
|
232
|
+
throw new Error('Ticket non trovato');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const actions = macro.actions;
|
|
236
|
+
const updates: any = { updated_at: Date.now() };
|
|
237
|
+
const executedActions: string[] = [];
|
|
238
|
+
|
|
239
|
+
if (actions.changeStatus) {
|
|
240
|
+
updates.status = actions.changeStatus;
|
|
241
|
+
executedActions.push(`Stato cambiato a: ${actions.changeStatus}`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (actions.assignSupplier) {
|
|
245
|
+
updates.supplierId = actions.assignSupplier;
|
|
246
|
+
executedActions.push('Fornitore assegnato');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (actions.assignUser) {
|
|
250
|
+
const user = await ctx.db.get(actions.assignUser);
|
|
251
|
+
if (user?.supplierId) {
|
|
252
|
+
updates.supplierId = user.supplierId;
|
|
253
|
+
}
|
|
254
|
+
executedActions.push('Utente assegnato');
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (actions.setSlaHours) {
|
|
258
|
+
updates.slaHours = actions.setSlaHours;
|
|
259
|
+
updates.slaDueDate = Date.now() + (actions.setSlaHours * 60 * 60 * 1000);
|
|
260
|
+
executedActions.push(`SLA impostato a ${actions.setSlaHours} ore`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (actions.setPriority) {
|
|
264
|
+
updates.priority = actions.setPriority;
|
|
265
|
+
executedActions.push(`Priorità impostata a: ${actions.setPriority}`);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
await ctx.db.patch(args.ticketId, updates);
|
|
269
|
+
|
|
270
|
+
if (actions.addNote) {
|
|
271
|
+
await ctx.db.insert('ticket_comments', {
|
|
272
|
+
taskId: args.ticketId,
|
|
273
|
+
authorId: args.userAuth0Id || 'system',
|
|
274
|
+
authorName: args.userName || args.userEmail || 'Sistema',
|
|
275
|
+
authorEmail: args.userEmail || 'system@app',
|
|
276
|
+
authorRole: 'admin',
|
|
277
|
+
content: `[Macro: ${macro.name}] ${actions.addNote}`,
|
|
278
|
+
isInternal: true,
|
|
279
|
+
createdAt: Date.now(),
|
|
280
|
+
});
|
|
281
|
+
executedActions.push('Nota aggiunta');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
success: true,
|
|
286
|
+
ticketId: args.ticketId,
|
|
287
|
+
macroName: macro.name,
|
|
288
|
+
executedActions,
|
|
289
|
+
};
|
|
290
|
+
},
|
|
291
|
+
});
|