Add tests and functions
Some checks failed
continuous-integration/drone/push Build encountered an error

Allow to run jests and add the excepted
Add not resolve yet for the async in tests
This commit is contained in:
2021-03-13 11:27:14 +07:00
parent fc4d8c9cdb
commit 0f3746f189
9 changed files with 137 additions and 30 deletions

View File

@@ -0,0 +1,7 @@
export default class ProfileRequest {
bio: string;
constructor(bio: string) {
this.bio = bio;
}
}

9
src/Model/UserRequest.ts Normal file
View File

@@ -0,0 +1,9 @@
export default class UserRequest {
name: string;
email: string;
constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
}

View File

@@ -1,9 +1,11 @@
import { PrismaClient, User } from "@prisma/client";
import ProfileRequest from "../Model/ProfileRequest";
import UserRequest from "../Model/UserRequest";
const prisma = new PrismaClient();
async function isProfileExistByEmail(email: string) {
const exist = await prisma.profile.count({
function isProfileExistByEmail(email: string) {
const exist = prisma.profile.count({
where: {
user: {
email: email,
@@ -11,7 +13,7 @@ async function isProfileExistByEmail(email: string) {
},
});
return exist > 0;
return exist.then((c) => c > 0).catch((e) => 0);
}
async function findOneProfileByEmail(email: string) {
@@ -21,19 +23,22 @@ async function findOneProfileByEmail(email: string) {
email: email,
},
},
include: {
user: true,
},
});
return profile;
}
async function isExistByEmail(email: string) {
const exist = await prisma.user.count({
function isExistByEmail(email: string) {
const exist = prisma.user.count({
where: {
email: email,
},
});
return exist > 0;
return exist.then((c) => c > 0).catch((e) => 0);
}
async function findOneByEmail(
@@ -53,18 +58,15 @@ async function findOneByEmail(
return user;
}
async function createUser() {
const email = "ops@cubetiqs.com";
if (isExistByEmail(email)) {
console.log("User existed with email => ", email);
return findOneByEmail(email);
async function createUser(request: UserRequest) {
if (isExistByEmail(request.email)) {
return findOneByEmail(request.email);
}
const user = await prisma.user.create({
data: {
name: "CUBETIQ Solution",
email: email,
name: request.name,
email: request.email,
},
});
@@ -73,16 +75,15 @@ async function createUser() {
return user;
}
async function createProfile(user: User) {
async function createProfile(user: User, request: ProfileRequest) {
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",
bio: request.bio,
},
});
@@ -91,4 +92,4 @@ async function createProfile(user: User) {
return profile;
}
export { createUser, createProfile };
export { createUser, createProfile, findOneByEmail, findOneProfileByEmail };

View File

@@ -5,12 +5,22 @@ const prisma = new PrismaClient();
// main function
async function main() {
const email = "ops@cubetiqs.com";
const name = "CUBETIQ Solution";
// create user
await createUser()
await createUser(
{
name: name,
email: email,
}
)
.then((user) => {
if (user != null) {
// create profile for user
createProfile(user);
createProfile(user, {
bio: "Software Developer",
});
}
})
.catch((e) => {