| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- "use server";
- import { prisma } from "@/lib/prisma";
- // Layout Configuration Actions
- export async function getLayoutConfigurations() {
- try {
- const configurations = await prisma.layoutConfiguration.findMany({
- include: {
- sections: {
- include: {
- fields: true,
- },
- },
- },
- orderBy: {
- createdAt: "desc",
- },
- });
- return { success: true, data: configurations };
- } catch (error) {
- console.error("Error fetching layout configurations:", error);
- return { success: false, error: "Failed to fetch layout configurations" };
- }
- }
- export async function getLayoutConfiguration(id: number) {
- try {
- const configuration = await prisma.layoutConfiguration.findUnique({
- where: { id },
- include: {
- sections: {
- include: {
- fields: true,
- },
- orderBy: {
- createdAt: "asc",
- },
- },
- },
- });
-
- if (!configuration) {
- return { success: false, error: "Layout configuration not found" };
- }
-
- return { success: true, data: configuration };
- } catch (error) {
- console.error("Error fetching layout configuration:", error);
- return { success: false, error: "Failed to fetch layout configuration" };
- }
- }
- export async function getLayoutSections(configurationId: number) {
- try {
- const sections = await prisma.layoutSection.findMany({
- where: { configurationId },
- include: {
- fields: {
- orderBy: {
- importColumnOrderNumber: "asc",
- },
- },
- },
- orderBy: {
- createdAt: "asc",
- },
- });
- return { success: true, data: sections };
- } catch (error) {
- console.error("Error fetching layout sections:", error);
- return { success: false, error: "Failed to fetch layout sections" };
- }
- }
- export async function getLayoutSectionFields(sectionId: number) {
- try {
- const fields = await prisma.layoutSectionField.findMany({
- where: { layoutSectionId: sectionId },
- orderBy: {
- importColumnOrderNumber: "asc",
- },
- });
- return { success: true, data: fields };
- } catch (error) {
- console.error("Error fetching layout section fields:", error);
- return { success: false, error: "Failed to fetch layout section fields" };
- }
- }
- // Get single layout section
- export async function getSection(id: number) {
- try {
- const section = await prisma.layoutSection.findUnique({
- where: { id },
- include: {
- fields: {
- orderBy: {
- importColumnOrderNumber: "asc",
- },
- },
- },
- });
-
- if (!section) {
- return { success: false, error: "Section not found" };
- }
-
- return { success: true, data: section };
- } catch (error) {
- console.error("Error fetching section:", error);
- return { success: false, error: "Failed to fetch section" };
- }
- }
- // Create new layout section field
- export async function createLayoutSectionField(
- sectionId: number,
- fieldData: {
- name: string;
- dataType: string;
- dataTypeFormat?: string;
- cellPosition: string;
- importTableColumnName: string;
- importColumnOrderNumber: number;
- }
- ) {
- try {
- const field = await prisma.layoutSectionField.create({
- data: {
- layoutSectionId: sectionId,
- name: fieldData.name,
- dataType: fieldData.dataType,
- dataTypeFormat: fieldData.dataTypeFormat || null,
- cellPosition: fieldData.cellPosition,
- importTableColumnName: fieldData.importTableColumnName,
- importColumnOrderNumber: fieldData.importColumnOrderNumber,
- },
- });
- return { success: true, data: field };
- } catch (error) {
- console.error("Error creating layout section field:", error);
- return { success: false, error: "Failed to create layout section field" };
- }
- }
- // Update layout section field
- export async function updateLayoutSectionField(
- id: number,
- fieldData: {
- name?: string;
- dataType?: string;
- dataTypeFormat?: string;
- cellPosition?: string;
- importTableColumnName?: string;
- importColumnOrderNumber?: number;
- }
- ) {
- try {
- const field = await prisma.layoutSectionField.update({
- where: { id },
- data: {
- ...(fieldData.name && { name: fieldData.name }),
- ...(fieldData.dataType && { dataType: fieldData.dataType }),
- ...(fieldData.dataTypeFormat !== undefined && { dataTypeFormat: fieldData.dataTypeFormat }),
- ...(fieldData.cellPosition && { cellPosition: fieldData.cellPosition }),
- ...(fieldData.importTableColumnName && { importTableColumnName: fieldData.importTableColumnName }),
- ...(fieldData.importColumnOrderNumber !== undefined && { importColumnOrderNumber: fieldData.importColumnOrderNumber }),
- },
- });
- return { success: true, data: field };
- } catch (error) {
- console.error("Error updating layout section field:", error);
- return { success: false, error: "Failed to update layout section field" };
- }
- }
- // Delete layout section field
- export async function deleteLayoutSectionField(id: number) {
- try {
- await prisma.layoutSectionField.delete({
- where: { id },
- });
- return { success: true };
- } catch (error) {
- console.error("Error deleting layout section field:", error);
- return { success: false, error: "Failed to delete layout section field" };
- }
- }
- // Create new layout configuration
- export async function createLayoutConfiguration(name: string) {
- try {
- const configuration = await prisma.layoutConfiguration.create({
- data: {
- name,
- },
- });
- return { success: true, data: configuration };
- } catch (error) {
- console.error("Error creating layout configuration:", error);
- return { success: false, error: "Failed to create layout configuration" };
- }
- }
- // Update layout configuration
- export async function updateLayoutConfiguration(id: number, name: string) {
- try {
- const configuration = await prisma.layoutConfiguration.update({
- where: { id },
- data: {
- name,
- },
- });
- return { success: true, data: configuration };
- } catch (error) {
- console.error("Error updating layout configuration:", error);
- return { success: false, error: "Failed to update layout configuration" };
- }
- }
- // Delete layout configuration
- export async function deleteLayoutConfiguration(id: number) {
- try {
- await prisma.layoutConfiguration.delete({
- where: { id },
- });
- return { success: true };
- } catch (error) {
- console.error("Error deleting layout configuration:", error);
- return { success: false, error: "Failed to delete layout configuration" };
- }
- }
- // Create new layout section
- export async function createLayoutSection(
- configurationId: number,
- sectionData: {
- name: string;
- type: string;
- sheetName: string;
- tableName: string;
- startingRow?: number;
- endingRow?: number;
- }
- ) {
- try {
- const section = await prisma.layoutSection.create({
- data: {
- configurationId,
- name: sectionData.name,
- type: sectionData.type,
- sheetName: sectionData.sheetName,
- tableName: sectionData.tableName,
- startingRow: sectionData.startingRow,
- endingRow: sectionData.endingRow,
- },
- });
- return { success: true, data: section };
- } catch (error) {
- console.error("Error creating layout section:", error);
- return { success: false, error: "Failed to create layout section" };
- }
- }
|