Kaynağa Gözat

improved workorder data

Bernt Christian Egeland 8 ay önce
ebeveyn
işleme
cac216761c

+ 23 - 0
src/app/api/protected/backup/import/route.ts

@@ -300,6 +300,11 @@ export async function POST(request: NextRequest) {
         });
         });
       }
       }
 
 
+      // Resolve work day start time from imported settings for backfilling service record times
+      const workDayStartSetting = (data.settings as Record<string, unknown>[] | undefined)
+        ?.find((s) => s.key === "workboard.workDayStart");
+      const workDayStartTime = (workDayStartSetting?.value as string) || "07:00";
+
       // 6. Insert vehicles with nested data
       // 6. Insert vehicles with nested data
       if (data.vehicles?.length) {
       if (data.vehicles?.length) {
         for (const v of data.vehicles as Record<string, unknown>[]) {
         for (const v of data.vehicles as Record<string, unknown>[]) {
@@ -409,6 +414,22 @@ export async function POST(request: NextRequest) {
             | undefined;
             | undefined;
           if (serviceRecords?.length) {
           if (serviceRecords?.length) {
             for (const sr of serviceRecords) {
             for (const sr of serviceRecords) {
+              // Derive startDateTime/endDateTime from backup or fall back to serviceDate + work day start
+              let startDT: Date | undefined;
+              let endDT: Date | undefined;
+              if (sr.startDateTime) {
+                startDT = new Date(sr.startDateTime as string);
+              } else if (sr.serviceDate) {
+                const sd = new Date(sr.serviceDate as string);
+                const [h, m] = workDayStartTime.split(":").map(Number);
+                startDT = new Date(sd.getFullYear(), sd.getMonth(), sd.getDate(), h, m, 0, 0);
+              }
+              if (sr.endDateTime) {
+                endDT = new Date(sr.endDateTime as string);
+              } else if (startDT) {
+                endDT = new Date(startDT.getTime() + 3600000);
+              }
+
               await tx.serviceRecord.create({
               await tx.serviceRecord.create({
                 data: {
                 data: {
                   id: sr.id as string,
                   id: sr.id as string,
@@ -421,6 +442,8 @@ export async function POST(request: NextRequest) {
                   serviceDate: sr.serviceDate
                   serviceDate: sr.serviceDate
                     ? new Date(sr.serviceDate as string)
                     ? new Date(sr.serviceDate as string)
                     : undefined,
                     : undefined,
+                  startDateTime: startDT ?? undefined,
+                  endDateTime: endDT ?? undefined,
                   shopName: (sr.shopName as string) || null,
                   shopName: (sr.shopName as string) || null,
                   techName: (sr.techName as string) || null,
                   techName: (sr.techName as string) || null,
                   parts: (sr.parts as string) || null,
                   parts: (sr.parts as string) || null,

+ 3 - 2
src/features/inspections/Actions/templateActions.ts

@@ -276,13 +276,14 @@ async function seedDefaultTemplateForOrg(organizationId: string) {
     },
     },
   });
   });
 
 
-  revalidatePath("/settings/inspections");
   return template;
   return template;
 }
 }
 
 
 export async function seedDefaultTemplate() {
 export async function seedDefaultTemplate() {
   return withAuth(async ({ organizationId }) => {
   return withAuth(async ({ organizationId }) => {
-    return seedDefaultTemplateForOrg(organizationId);
+    const result = await seedDefaultTemplateForOrg(organizationId);
+    revalidatePath("/settings/inspections");
+    return result;
   }, { requiredPermissions: [{ action: PermissionAction.CREATE, subject: PermissionSubject.INSPECTIONS }] });
   }, { requiredPermissions: [{ action: PermissionAction.CREATE, subject: PermissionSubject.INSPECTIONS }] });
 }
 }
 
 

+ 16 - 3
src/features/vehicles/Actions/createDraftServiceRecord.ts

@@ -26,6 +26,7 @@ export async function createDraftServiceRecord(
                 "workshop.invoicePrefix",
                 "workshop.invoicePrefix",
                 "workshop.invoiceStartNumber",
                 "workshop.invoiceStartNumber",
                 "workshop.defaultTechnician",
                 "workshop.defaultTechnician",
+                "workboard.workDayStart",
               ],
               ],
             },
             },
           },
           },
@@ -99,6 +100,18 @@ export async function createDraftServiceRecord(
         });
         });
       }
       }
 
 
+      // Default to today's date at work day start time (from settings, fallback 07:00)
+      let defaultStart: Date;
+      if (!startDateTime) {
+        const workDayStart = settingsMap["workboard.workDayStart"] || "07:00";
+        const [h, m] = workDayStart.split(":").map(Number);
+        defaultStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), h, m, 0, 0);
+      } else {
+        defaultStart = startDateTime;
+      }
+      // serviceDate should be date-only (start of day)
+      const serviceDate = new Date(defaultStart.getFullYear(), defaultStart.getMonth(), defaultStart.getDate());
+
       const record = await db.serviceRecord.create({
       const record = await db.serviceRecord.create({
         data: {
         data: {
           title: "New Service Record",
           title: "New Service Record",
@@ -109,9 +122,9 @@ export async function createDraftServiceRecord(
           techName,
           techName,
           technicianId: resolvedTechId || undefined,
           technicianId: resolvedTechId || undefined,
           invoiceNumber,
           invoiceNumber,
-          serviceDate: startDateTime ?? now,
-          startDateTime: startDateTime ?? now,
-          endDateTime: endDateTime ?? new Date((startDateTime ?? now).getTime() + 3600000),
+          serviceDate,
+          startDateTime: defaultStart,
+          endDateTime: endDateTime ?? new Date(defaultStart.getTime() + 3600000),
         },
         },
       });
       });
 
 

+ 6 - 2
src/features/workboard/Actions/boardActions/assignments.ts

@@ -209,10 +209,14 @@ export async function assignTechnician(input: unknown) {
 
 
       let job: WorkBoardJob;
       let job: WorkBoardJob;
 
 
+      const timeData = data.startDateTime && data.endDateTime
+        ? { startDateTime: data.startDateTime, endDateTime: data.endDateTime }
+        : {};
+
       if (data.type === "serviceRecord") {
       if (data.type === "serviceRecord") {
         const sr = await db.serviceRecord.update({
         const sr = await db.serviceRecord.update({
           where: { id: data.id },
           where: { id: data.id },
-          data: { technicianId: data.technicianId, techName: tech.name },
+          data: { technicianId: data.technicianId, techName: tech.name, ...timeData },
           select: {
           select: {
             id: true,
             id: true,
             title: true,
             title: true,
@@ -228,7 +232,7 @@ export async function assignTechnician(input: unknown) {
       } else {
       } else {
         const insp = await db.inspection.update({
         const insp = await db.inspection.update({
           where: { id: data.id },
           where: { id: data.id },
-          data: { technicianId: data.technicianId },
+          data: { technicianId: data.technicianId, ...timeData },
           select: {
           select: {
             id: true,
             id: true,
             status: true,
             status: true,

+ 5 - 1
src/features/workboard/Components/WorkBoardClient.tsx

@@ -178,8 +178,12 @@ export function WorkBoardClient({
     if (activeData?.job && activeData?.type) {
     if (activeData?.job && activeData?.type) {
       const job = activeData.job;
       const job = activeData.job;
       const jobType = activeData.type as "serviceRecord" | "inspection";
       const jobType = activeData.type as "serviceRecord" | "inspection";
+      // Set default time on the drop target's date (1 hour block at work day start)
+      const dropDate = overData.date || selectedDate;
+      const startDateTime = new Date(dropDate + "T" + boardSettings.workDayStart + ":00");
+      const endDateTime = new Date(startDateTime.getTime() + 60 * 60 * 1000);
       store.removeFromUnassigned(job.id, jobType);
       store.removeFromUnassigned(job.id, jobType);
-      const res = await assignTechnician({ id: job.id, technicianId: overData.technicianId, type: jobType });
+      const res = await assignTechnician({ id: job.id, technicianId: overData.technicianId, type: jobType, startDateTime, endDateTime });
       if (!res.success) { toast.error(t("failedAssign"), { description: res.error }); store.addToUnassigned(job, jobType); }
       if (!res.success) { toast.error(t("failedAssign"), { description: res.error }); store.addToUnassigned(job, jobType); }
       else if (res.data) store.addJob(res.data as WorkBoardJob);
       else if (res.data) store.addJob(res.data as WorkBoardJob);
     }
     }

+ 2 - 0
src/features/workboard/Schema/workboardSchema.ts

@@ -26,6 +26,8 @@ export const assignTechnicianSchema = z.object({
   id: z.string().min(1),
   id: z.string().min(1),
   technicianId: z.string().min(1),
   technicianId: z.string().min(1),
   type: z.enum(["serviceRecord", "inspection"]),
   type: z.enum(["serviceRecord", "inspection"]),
+  startDateTime: z.coerce.date().optional(),
+  endDateTime: z.coerce.date().optional(),
 });
 });
 
 
 export const moveJobSchema = z.object({
 export const moveJobSchema = z.object({