From fc4d8c9cdbab79ad8364cecd9eedfe6176865349 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 13 Mar 2021 10:37:40 +0700 Subject: [PATCH] Add user service and implements for internal functions and exports# --- src/User/UserService.ts | 94 +++++++++++++++++++++++++++++++++++++++++ src/main.ts | 35 +++------------ test/user.test.ts | 9 ++++ 3 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 src/User/UserService.ts create mode 100644 test/user.test.ts diff --git a/src/User/UserService.ts b/src/User/UserService.ts new file mode 100644 index 0000000..7490698 --- /dev/null +++ b/src/User/UserService.ts @@ -0,0 +1,94 @@ +import { PrismaClient, User } from "@prisma/client"; + +const prisma = new PrismaClient(); + +async function isProfileExistByEmail(email: string) { + const exist = await prisma.profile.count({ + where: { + user: { + email: email, + }, + }, + }); + + return exist > 0; +} + +async function findOneProfileByEmail(email: string) { + const profile = await prisma.profile.findFirst({ + where: { + user: { + email: email, + }, + }, + }); + + return profile; +} + +async function isExistByEmail(email: string) { + const exist = await prisma.user.count({ + where: { + email: email, + }, + }); + + return exist > 0; +} + +async function findOneByEmail( + email: string, + throwableIfNotFound: boolean = false +) { + const user = await prisma.user.findFirst({ + where: { + email: email, + }, + }); + + if (user == null && throwableIfNotFound) { + throw Error(`user not found by email: ${email}!`); + } + + return user; +} + +async function createUser() { + const email = "ops@cubetiqs.com"; + + if (isExistByEmail(email)) { + console.log("User existed with email => ", email); + return findOneByEmail(email); + } + + const user = await prisma.user.create({ + data: { + name: "CUBETIQ Solution", + email: email, + }, + }); + + console.log("User created => ", user); + + return user; +} + +async function createProfile(user: User) { + if (isProfileExistByEmail(user.email)) { + console.log("Profile existed by email => ", user.email); + return findOneProfileByEmail(user.email); + } + + const profile = await prisma.profile.create({ + data: { + userId: user.id, + bio: "Software Company", + }, + }); + + console.log("Profile created => ", profile); + + return profile; +} + +export { createUser, createProfile }; diff --git a/src/main.ts b/src/main.ts index e235c18..1457a02 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,14 +1,17 @@ -import { PrismaClient, User } from "@prisma/client"; +import { PrismaClient } from "@prisma/client"; +import { createUser, createProfile } from "./User/UserService"; const prisma = new PrismaClient(); -// A `main` function so that you can use async/await +// main function async function main() { // create user await createUser() .then((user) => { - // create profile for user - createProfile(user); + if (user != null) { + // create profile for user + createProfile(user); + } }) .catch((e) => { console.error(e); @@ -18,30 +21,6 @@ async function main() { console.log("All users => ", allUsers); } -async function createUser() { - const user = await prisma.user.create({ - data: { - name: "CUBETIQ Solution", - email: "ops@cubetiqs.com", - }, - }); - - console.log("User => ", user); - - return user; -} - -async function createProfile(user: User) { - const profile = await prisma.profile.create({ - data: { - userId: user.id, - bio: "Software Company", - }, - }); - - console.log("Profile => ", profile); -} - main() .catch((e) => { throw e; diff --git a/test/user.test.ts b/test/user.test.ts new file mode 100644 index 0000000..683f991 --- /dev/null +++ b/test/user.test.ts @@ -0,0 +1,9 @@ +describe('user', function() { + it('createUser', function() { + + }) + + it('checkUser', function() { + + }) +}) \ No newline at end of file