imports.ts 5.0 KB

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