layout-configurations.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. "use server";
  2. import { prisma } from "@/lib/prisma";
  3. // Layout Configuration Actions
  4. export async function getLayoutConfigurations() {
  5. try {
  6. const configurations = await prisma.layoutConfiguration.findMany({
  7. include: {
  8. sections: {
  9. include: {
  10. fields: true,
  11. },
  12. },
  13. },
  14. orderBy: {
  15. createdAt: "desc",
  16. },
  17. });
  18. return { success: true, data: configurations };
  19. } catch (error) {
  20. console.error("Error fetching layout configurations:", error);
  21. return { success: false, error: "Failed to fetch layout configurations" };
  22. }
  23. }
  24. export async function getLayoutConfiguration(id: number) {
  25. try {
  26. const configuration = await prisma.layoutConfiguration.findUnique({
  27. where: { id },
  28. include: {
  29. sections: {
  30. include: {
  31. fields: true,
  32. },
  33. orderBy: {
  34. createdAt: "asc",
  35. },
  36. },
  37. },
  38. });
  39. if (!configuration) {
  40. return { success: false, error: "Layout configuration not found" };
  41. }
  42. return { success: true, data: configuration };
  43. } catch (error) {
  44. console.error("Error fetching layout configuration:", error);
  45. return { success: false, error: "Failed to fetch layout configuration" };
  46. }
  47. }
  48. export async function getLayoutSections(configurationId: number) {
  49. try {
  50. const sections = await prisma.layoutSection.findMany({
  51. where: { configurationId },
  52. include: {
  53. fields: {
  54. orderBy: {
  55. importColumnOrderNumber: "asc",
  56. },
  57. },
  58. },
  59. orderBy: {
  60. createdAt: "asc",
  61. },
  62. });
  63. return { success: true, data: sections };
  64. } catch (error) {
  65. console.error("Error fetching layout sections:", error);
  66. return { success: false, error: "Failed to fetch layout sections" };
  67. }
  68. }
  69. export async function getLayoutSectionFields(sectionId: number) {
  70. try {
  71. const fields = await prisma.layoutSectionField.findMany({
  72. where: { layoutSectionId: sectionId },
  73. orderBy: {
  74. importColumnOrderNumber: "asc",
  75. },
  76. });
  77. return { success: true, data: fields };
  78. } catch (error) {
  79. console.error("Error fetching layout section fields:", error);
  80. return { success: false, error: "Failed to fetch layout section fields" };
  81. }
  82. }
  83. // Get single layout section
  84. export async function getSection(id: number) {
  85. try {
  86. const section = await prisma.layoutSection.findUnique({
  87. where: { id },
  88. include: {
  89. fields: {
  90. orderBy: {
  91. importColumnOrderNumber: "asc",
  92. },
  93. },
  94. },
  95. });
  96. if (!section) {
  97. return { success: false, error: "Section not found" };
  98. }
  99. return { success: true, data: section };
  100. } catch (error) {
  101. console.error("Error fetching section:", error);
  102. return { success: false, error: "Failed to fetch section" };
  103. }
  104. }
  105. // Create new layout section field
  106. export async function createLayoutSectionField(
  107. sectionId: number,
  108. fieldData: {
  109. name: string;
  110. dataType: string;
  111. dataTypeFormat?: string;
  112. cellPosition: string;
  113. importTableColumnName: string;
  114. importColumnOrderNumber: number;
  115. }
  116. ) {
  117. try {
  118. const field = await prisma.layoutSectionField.create({
  119. data: {
  120. layoutSectionId: sectionId,
  121. name: fieldData.name,
  122. dataType: fieldData.dataType,
  123. dataTypeFormat: fieldData.dataTypeFormat || null,
  124. cellPosition: fieldData.cellPosition,
  125. importTableColumnName: fieldData.importTableColumnName,
  126. importColumnOrderNumber: fieldData.importColumnOrderNumber,
  127. },
  128. });
  129. return { success: true, data: field };
  130. } catch (error) {
  131. console.error("Error creating layout section field:", error);
  132. return { success: false, error: "Failed to create layout section field" };
  133. }
  134. }
  135. // Update layout section field
  136. export async function updateLayoutSectionField(
  137. id: number,
  138. fieldData: {
  139. name?: string;
  140. dataType?: string;
  141. dataTypeFormat?: string;
  142. cellPosition?: string;
  143. importTableColumnName?: string;
  144. importColumnOrderNumber?: number;
  145. }
  146. ) {
  147. try {
  148. const field = await prisma.layoutSectionField.update({
  149. where: { id },
  150. data: {
  151. ...(fieldData.name && { name: fieldData.name }),
  152. ...(fieldData.dataType && { dataType: fieldData.dataType }),
  153. ...(fieldData.dataTypeFormat !== undefined && { dataTypeFormat: fieldData.dataTypeFormat }),
  154. ...(fieldData.cellPosition && { cellPosition: fieldData.cellPosition }),
  155. ...(fieldData.importTableColumnName && { importTableColumnName: fieldData.importTableColumnName }),
  156. ...(fieldData.importColumnOrderNumber !== undefined && { importColumnOrderNumber: fieldData.importColumnOrderNumber }),
  157. },
  158. });
  159. return { success: true, data: field };
  160. } catch (error) {
  161. console.error("Error updating layout section field:", error);
  162. return { success: false, error: "Failed to update layout section field" };
  163. }
  164. }
  165. // Delete layout section field
  166. export async function deleteLayoutSectionField(id: number) {
  167. try {
  168. await prisma.layoutSectionField.delete({
  169. where: { id },
  170. });
  171. return { success: true };
  172. } catch (error) {
  173. console.error("Error deleting layout section field:", error);
  174. return { success: false, error: "Failed to delete layout section field" };
  175. }
  176. }
  177. // Create new layout configuration
  178. export async function createLayoutConfiguration(name: string) {
  179. try {
  180. const configuration = await prisma.layoutConfiguration.create({
  181. data: {
  182. name,
  183. },
  184. });
  185. return { success: true, data: configuration };
  186. } catch (error) {
  187. console.error("Error creating layout configuration:", error);
  188. return { success: false, error: "Failed to create layout configuration" };
  189. }
  190. }
  191. // Update layout configuration
  192. export async function updateLayoutConfiguration(id: number, name: string) {
  193. try {
  194. const configuration = await prisma.layoutConfiguration.update({
  195. where: { id },
  196. data: {
  197. name,
  198. },
  199. });
  200. return { success: true, data: configuration };
  201. } catch (error) {
  202. console.error("Error updating layout configuration:", error);
  203. return { success: false, error: "Failed to update layout configuration" };
  204. }
  205. }
  206. // Delete layout configuration
  207. export async function deleteLayoutConfiguration(id: number) {
  208. try {
  209. await prisma.layoutConfiguration.delete({
  210. where: { id },
  211. });
  212. return { success: true };
  213. } catch (error) {
  214. console.error("Error deleting layout configuration:", error);
  215. return { success: false, error: "Failed to delete layout configuration" };
  216. }
  217. }
  218. // Create new layout section
  219. export async function createLayoutSection(
  220. configurationId: number,
  221. sectionData: {
  222. name: string;
  223. type: string;
  224. sheetName: string;
  225. tableName: string;
  226. startingRow?: number;
  227. endingRow?: number;
  228. }
  229. ) {
  230. try {
  231. const section = await prisma.layoutSection.create({
  232. data: {
  233. configurationId,
  234. name: sectionData.name,
  235. type: sectionData.type,
  236. sheetName: sectionData.sheetName,
  237. tableName: sectionData.tableName,
  238. startingRow: sectionData.startingRow,
  239. endingRow: sectionData.endingRow,
  240. },
  241. });
  242. return { success: true, data: section };
  243. } catch (error) {
  244. console.error("Error creating layout section:", error);
  245. return { success: false, error: "Failed to create layout section" };
  246. }
  247. }