refractor code,
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import { prisma } from '../../lib/prisma'
|
||||
|
||||
type DbClient = Pick<typeof prisma, 'company' | 'brandSettings' | 'contractSettings' | 'subscription' | 'employee'>
|
||||
|
||||
type CompanySignupInput = {
|
||||
companyName: string
|
||||
slug: string
|
||||
email: string
|
||||
companyPhone: string
|
||||
streetAddress: string
|
||||
city: string
|
||||
country: string
|
||||
zipCode: string
|
||||
legalForm: string
|
||||
managerName: string
|
||||
fax?: string
|
||||
yearsActive: string
|
||||
currency: 'MAD' | 'USD' | 'EUR'
|
||||
registrationNumber: string
|
||||
plan: 'STARTER' | 'GROWTH' | 'PRO'
|
||||
billingPeriod: 'MONTHLY' | 'ANNUAL'
|
||||
preferredLanguage: 'en' | 'fr' | 'ar'
|
||||
firstName: string
|
||||
lastName: string
|
||||
passwordHash: string
|
||||
now: Date
|
||||
trialEndAt: Date
|
||||
}
|
||||
|
||||
export function findCompanyBySlug(slug: string) {
|
||||
return prisma.company.findUnique({ where: { slug } })
|
||||
}
|
||||
|
||||
export function findCompanyByEmail(email: string) {
|
||||
return prisma.company.findUnique({ where: { email } })
|
||||
}
|
||||
|
||||
export function findEmployeeByEmail(email: string) {
|
||||
return prisma.employee.findFirst({ where: { email } })
|
||||
}
|
||||
|
||||
export async function createCompanySignup(db: DbClient, input: CompanySignupInput) {
|
||||
const company = await db.company.create({
|
||||
data: {
|
||||
name: input.companyName,
|
||||
slug: input.slug,
|
||||
email: input.email,
|
||||
phone: input.companyPhone,
|
||||
address: {
|
||||
streetAddress: input.streetAddress,
|
||||
city: input.city,
|
||||
country: input.country,
|
||||
zipCode: input.zipCode,
|
||||
legalForm: input.legalForm,
|
||||
managerName: input.managerName,
|
||||
fax: input.fax || null,
|
||||
yearsActive: input.yearsActive,
|
||||
},
|
||||
status: 'TRIALING',
|
||||
},
|
||||
})
|
||||
|
||||
await db.brandSettings.create({
|
||||
data: {
|
||||
companyId: company.id,
|
||||
displayName: input.companyName,
|
||||
subdomain: input.slug,
|
||||
publicEmail: input.email,
|
||||
publicPhone: input.companyPhone,
|
||||
publicAddress: input.streetAddress,
|
||||
publicCity: input.city,
|
||||
publicCountry: input.country,
|
||||
defaultCurrency: input.currency,
|
||||
},
|
||||
})
|
||||
|
||||
await db.contractSettings.create({
|
||||
data: {
|
||||
companyId: company.id,
|
||||
legalName: input.companyName,
|
||||
registrationNumber: input.registrationNumber,
|
||||
},
|
||||
})
|
||||
|
||||
await db.subscription.create({
|
||||
data: {
|
||||
companyId: company.id,
|
||||
plan: input.plan,
|
||||
billingPeriod: input.billingPeriod,
|
||||
currency: input.currency,
|
||||
status: 'TRIALING',
|
||||
trialStartAt: input.now,
|
||||
trialEndAt: input.trialEndAt,
|
||||
currentPeriodStart: input.now,
|
||||
currentPeriodEnd: input.trialEndAt,
|
||||
},
|
||||
})
|
||||
|
||||
const employee = await db.employee.create({
|
||||
data: {
|
||||
companyId: company.id,
|
||||
clerkUserId: `local_owner_${company.id}`,
|
||||
firstName: input.firstName,
|
||||
lastName: input.lastName,
|
||||
email: input.email,
|
||||
phone: input.companyPhone,
|
||||
passwordHash: input.passwordHash,
|
||||
role: 'OWNER',
|
||||
preferredLanguage: input.preferredLanguage,
|
||||
isActive: true,
|
||||
},
|
||||
})
|
||||
|
||||
return { company, employee }
|
||||
}
|
||||
Reference in New Issue
Block a user