Created
February 13, 2023 22:32
-
-
Save sacsbrainz/6d5a70799ded52de777068123acea125 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
signUp: publicProcedure | |
.input(registerSchema) | |
.mutation(async ({ input, ctx }) => { | |
const transformedPhoneNumber = input.phoneNumber.replace(/^0/, "+234"); | |
// check if user exists | |
const exists = await ctx.prisma.user.findUnique({ | |
where: { | |
email: input.email, | |
}, | |
}); | |
if (exists) { | |
throw new TRPCError({ | |
code: "CONFLICT", | |
message: "User already exists.", | |
}); | |
} | |
// individual | |
if (input.userType === "INDIVIDUAL") { | |
const sudoCustomer = await sudoApi({ | |
method: "POST", | |
url: "/customers", | |
data: { | |
type: "individual", | |
name: `${input.firstName} ${input.lastName}`, | |
status: "active", | |
phoneNumber: transformedPhoneNumber, | |
emailAddress: input.email, | |
individual: { | |
firstName: input.firstName, | |
lastName: input.lastName, | |
dob: input.dob, | |
identity: { | |
type: "BVN", | |
number: input.bvn, | |
}, | |
}, | |
billingAddress: { | |
line1: input.address1, | |
line2: input.address2, | |
city: input.city, | |
state: input.state, | |
country: input.country, | |
postalCode: input.postalCode, | |
}, | |
}, | |
}); | |
if (sudoCustomer.statusCode !== 200) { | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
} | |
const sudoCustomerWallet = await sudoApi({ | |
method: "POST", | |
url: "/accounts", | |
data: { | |
currency: "NGN", | |
type: "wallet", | |
accountType: "Current", | |
customerId: sudoCustomer.data._id, | |
}, | |
}); | |
if (sudoCustomerWallet.statusCode !== 200) { | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
} | |
const hashedPassword = await hash(input.pin); | |
const createUser = await ctx.prisma.user.create({ | |
data: { | |
firstName: input.firstName, | |
lastName: input.lastName, | |
email: input.email, | |
phoneNumber: input.phoneNumber, | |
address1: input.address1, | |
address2: input.address2, | |
customerType: input.userType, | |
city: input.city, | |
state: input.state, | |
country: input.country, | |
postalCode: input.postalCode, | |
pin: hashedPassword, | |
dob: new Date(input.dob), | |
sudoCustomerID: sudoCustomer.data._id, | |
companyName: undefined, | |
}, | |
}); | |
if (!createUser) | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
const createUserWallet = await ctx.prisma.wallet.create({ | |
data: { | |
sudoWalletID: sudoCustomerWallet.data._id, | |
user: { | |
connect: { | |
id: createUser.id, | |
}, | |
}, | |
}, | |
}); | |
if (!createUserWallet) | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
return { | |
code: 200, | |
status: "success", | |
message: "User created successfully", | |
}; | |
} | |
// company | |
const sudoCustomer = await sudoApi({ | |
method: "POST", | |
url: "/customers", | |
data: { | |
type: "company", | |
name: input.companyName, | |
status: "active", | |
phoneNumber: transformedPhoneNumber, | |
emailAddress: input.email, | |
identity: { | |
number: input.companyCac, | |
type: "CAC", | |
}, | |
company: { | |
name: input.companyName, | |
officer: { | |
firstName: input.firstName, | |
lastName: input.lastName, | |
dob: input.dob, | |
identity: { | |
type: "BVN", | |
number: input.bvn, | |
}, | |
}, | |
}, | |
billingAddress: { | |
line1: input.address1, | |
line2: input.address2, | |
city: input.city, | |
state: input.state, | |
country: input.country, | |
postalCode: input.postalCode, | |
}, | |
}, | |
}); | |
if (sudoCustomer.statusCode !== 200) { | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
} | |
const sudoCustomerWallet = await sudoApi({ | |
method: "POST", | |
url: "/accounts", | |
data: { | |
currency: "NGN", | |
type: "wallet", | |
accountType: "Current", | |
customerId: sudoCustomer.data._id, | |
}, | |
}); | |
if (sudoCustomerWallet.statusCode !== 200) { | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
} | |
const hashedPassword = await hash(input.pin); | |
const createUser = await ctx.prisma.user.create({ | |
data: { | |
firstName: input.firstName, | |
lastName: input.lastName, | |
email: input.email, | |
phoneNumber: input.phoneNumber, | |
address1: input.address1, | |
address2: input.address2, | |
customerType: input.userType, | |
city: input.city, | |
state: input.state, | |
country: input.country, | |
postalCode: input.postalCode, | |
pin: hashedPassword, | |
dob: new Date(input.dob), | |
sudoCustomerID: sudoCustomer.data._id, | |
companyName: input.companyName, | |
}, | |
}); | |
if (!createUser) | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
const createUserWallet = await ctx.prisma.wallet.create({ | |
data: { | |
sudoWalletID: sudoCustomerWallet.data._id, | |
user: { | |
connect: { | |
id: createUser.id, | |
}, | |
}, | |
}, | |
}); | |
if (!createUserWallet) | |
throw new TRPCError({ | |
code: "INTERNAL_SERVER_ERROR", | |
message: "Something went wrong", | |
}); | |
return { | |
code: 200, | |
status: "success", | |
message: "User created successfully", | |
}; | |
}), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment