imports.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. 'use server';
  2. import { prisma } from '@/lib/prisma';
  3. import { revalidatePath } from 'next/cache';
  4. import { z } from 'zod';
  5. // Validation schemas
  6. const createImportSchema = z.object({
  7. name: z.string().min(1, 'Import name is required'),
  8. layoutId: z.number().int().positive('Layout configuration is required'),
  9. fileId: z.string().optional(),
  10. });
  11. const updateImportSchema = z.object({
  12. id: z.number().int().positive(),
  13. name: z.string().min(1, 'Import name is required'),
  14. fileId: z.string().optional(),
  15. });
  16. // Create a new import
  17. export async function createImport(data: {
  18. name: string;
  19. layoutId: number;
  20. fileId?: string;
  21. }) {
  22. try {
  23. const validatedData = createImportSchema.parse(data);
  24. const importRecord = await prisma.import.create({
  25. data: {
  26. name: validatedData.name,
  27. layoutId: validatedData.layoutId,
  28. importDate: new Date(),
  29. ...(validatedData.fileId && { fileId: validatedData.fileId }),
  30. },
  31. include: {
  32. layout: true,
  33. },
  34. });
  35. revalidatePath('/imports');
  36. return { success: true, data: importRecord };
  37. } catch (error) {
  38. console.error('Error creating import:', error);
  39. return { success: false, error: 'Failed to create import' };
  40. }
  41. }
  42. // Get all imports
  43. export async function getImports() {
  44. try {
  45. const imports = await prisma.import.findMany({
  46. include: {
  47. layout: true,
  48. },
  49. orderBy: {
  50. importDate: 'desc',
  51. },
  52. });
  53. return { success: true, data: imports };
  54. } catch (error) {
  55. console.error('Error fetching imports:', error);
  56. return { success: false, error: 'Failed to fetch imports' };
  57. }
  58. }
  59. // Get a single import by ID
  60. export async function getImportById(id: number) {
  61. try {
  62. const importRecord = await prisma.import.findUnique({
  63. where: { id },
  64. include: {
  65. layout: {
  66. include: {
  67. sections: {
  68. include: {
  69. fields: true,
  70. },
  71. },
  72. },
  73. },
  74. cintasSummaries: {
  75. orderBy: {
  76. weekId: 'desc',
  77. },
  78. },
  79. },
  80. });
  81. if (!importRecord) {
  82. return { success: false, error: 'Import not found' };
  83. }
  84. return { success: true, data: importRecord };
  85. } catch (error) {
  86. console.error('Error fetching import:', error);
  87. return { success: false, error: 'Failed to fetch import' };
  88. }
  89. }
  90. // Update an import
  91. export async function updateImport(data: {
  92. id: number;
  93. name: string;
  94. fileId?: string;
  95. }) {
  96. try {
  97. const validatedData = updateImportSchema.parse(data);
  98. const importRecord = await prisma.import.update({
  99. where: { id: validatedData.id },
  100. data: {
  101. name: validatedData.name,
  102. ...(validatedData.fileId !== undefined && { fileId: validatedData.fileId }),
  103. },
  104. include: {
  105. layout: true,
  106. },
  107. });
  108. revalidatePath('/imports');
  109. return { success: true, data: importRecord };
  110. } catch (error) {
  111. console.error('Error updating import:', error);
  112. return { success: false, error: 'Failed to update import' };
  113. }
  114. }
  115. // Delete an import
  116. export async function deleteImport(id: number) {
  117. try {
  118. await prisma.import.delete({
  119. where: { id },
  120. });
  121. revalidatePath('/imports');
  122. return { success: true };
  123. } catch (error) {
  124. console.error('Error deleting import:', error);
  125. return { success: false, error: 'Failed to delete import' };
  126. }
  127. }
  128. // Calculate Cintas summaries for an import
  129. export async function calculateCintasSummaries(importId: number) {
  130. try {
  131. // This would typically call a stored procedure or perform calculations
  132. // For now, we'll simulate the calculation
  133. // In a real implementation, you might call:
  134. // await prisma.$executeRaw`CALL cintas_calculate_summary(${importId})`;
  135. // For demo purposes, we'll create some sample data
  136. const summaries = [
  137. {
  138. importId,
  139. week: '2024-W01',
  140. trrTotal: 100,
  141. fourWkAverages: 95,
  142. trrPlus4Wk: 195,
  143. powerAdds: 25,
  144. weekId: 1,
  145. },
  146. {
  147. importId,
  148. week: '2024-W02',
  149. trrTotal: 110,
  150. fourWkAverages: 100,
  151. trrPlus4Wk: 210,
  152. powerAdds: 30,
  153. weekId: 2,
  154. },
  155. ];
  156. // Clear existing summaries for this import
  157. await prisma.cintasSummary.deleteMany({
  158. where: { importId },
  159. });
  160. // Create new summaries
  161. const createdSummaries = await Promise.all(
  162. summaries.map(summary =>
  163. prisma.cintasSummary.create({
  164. data: summary,
  165. })
  166. )
  167. );
  168. return { success: true, data: createdSummaries };
  169. } catch (error) {
  170. console.error('Error calculating Cintas summaries:', error);
  171. return { success: false, error: 'Failed to calculate summaries' };
  172. }
  173. }
  174. // Get available layout configurations
  175. export async function getLayoutConfigurations() {
  176. try {
  177. const layouts = await prisma.layoutConfiguration.findMany({
  178. include: {
  179. sections: {
  180. include: {
  181. fields: true,
  182. },
  183. },
  184. },
  185. orderBy: {
  186. name: 'asc',
  187. },
  188. });
  189. return { success: true, data: layouts };
  190. } catch (error) {
  191. console.error('Error fetching layout configurations:', error);
  192. return { success: false, error: 'Failed to fetch layout configurations' };
  193. }
  194. }