11"use server" ;
22import prisma from "@/lib/db" ;
3+ import { AddCompanyFormSchema } from "@/models/addCompanyForm.schema" ;
34import { getCurrentUser } from "@/utils/user.utils" ;
5+ import { z } from "zod" ;
46
57export const getCompanyList = async (
68 page = 1 ,
@@ -84,9 +86,8 @@ export const getAllCompanies = async (): Promise<any | undefined> => {
8486 }
8587} ;
8688
87- export const createCompany = async (
88- label : string ,
89- logoUrl ?: string
89+ export const addCompany = async (
90+ data : z . infer < typeof AddCompanyFormSchema >
9091) : Promise < any | undefined > => {
9192 try {
9293 const user = await getCurrentUser ( ) ;
@@ -95,20 +96,85 @@ export const createCompany = async (
9596 throw new Error ( "Not authenticated" ) ;
9697 }
9798
98- const value = label . trim ( ) . toLowerCase ( ) ;
99+ const { company , logoUrl } = data ;
99100
100- // Upsert the name (create if it does not exist, update if it exists)
101- const upsertedName = await prisma . company . upsert ( {
102- where : { value } ,
103- update : { label, value, logoUrl } ,
104- create : { label, value, logoUrl, createdBy : user . id } ,
101+ const value = company . trim ( ) . toLowerCase ( ) ;
102+
103+ const companyExists = await prisma . company . findUnique ( {
104+ where : {
105+ value,
106+ } ,
107+ } ) ;
108+
109+ if ( companyExists ) {
110+ throw new Error ( "Company already exists!" ) ;
111+ }
112+
113+ const res = await prisma . company . create ( {
114+ data : {
115+ createdBy : user . id ,
116+ value,
117+ label : company ,
118+ logoUrl,
119+ } ,
105120 } ) ;
106121
107- return upsertedName ;
122+ return { success : true , data : res } ;
108123 } catch ( error ) {
109- const msg = "Failed to create company. " ;
124+ const msg = "Failed to create company." ;
110125 console . error ( msg , error ) ;
111- throw new Error ( msg ) ;
126+ if ( error instanceof Error ) {
127+ return { success : false , message : error . message } ;
128+ }
129+ }
130+ } ;
131+
132+ export const updateCompany = async (
133+ data : z . infer < typeof AddCompanyFormSchema >
134+ ) : Promise < any | undefined > => {
135+ try {
136+ const user = await getCurrentUser ( ) ;
137+
138+ if ( ! user ) {
139+ throw new Error ( "Not authenticated" ) ;
140+ }
141+
142+ const { id, company, logoUrl, createdBy } = data ;
143+
144+ if ( ! id || user . id != createdBy ) {
145+ throw new Error ( "Id is not provided or no user privilages" ) ;
146+ }
147+
148+ const value = company . trim ( ) . toLowerCase ( ) ;
149+
150+ const companyExists = await prisma . company . findUnique ( {
151+ where : {
152+ value,
153+ } ,
154+ } ) ;
155+
156+ if ( companyExists ) {
157+ throw new Error ( "Company already exists!" ) ;
158+ }
159+
160+ const res = await prisma . company . update ( {
161+ where : {
162+ id,
163+ } ,
164+ data : {
165+ value,
166+ label : company ,
167+ logoUrl,
168+ } ,
169+ } ) ;
170+
171+ return { success : true , data : res } ;
172+ } catch ( error ) {
173+ const msg = "Failed to update company." ;
174+ console . error ( msg , error ) ;
175+ if ( error instanceof Error ) {
176+ return { success : false , message : error . message } ;
177+ }
112178 }
113179} ;
114180
0 commit comments