Fixed tests and service for async
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
2a4beb0029
commit
5190435b68
BIN
prisma/dev.db
BIN
prisma/dev.db
Binary file not shown.
BIN
prisma/dev.db-journal
Normal file
BIN
prisma/dev.db-journal
Normal file
Binary file not shown.
20
prisma/migrations/20210924101547_add_profile/migration.sql
Normal file
20
prisma/migrations/20210924101547_add_profile/migration.sql
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
-- RedefineTables
|
||||||
|
PRAGMA foreign_keys=OFF;
|
||||||
|
CREATE TABLE "new_Profile" (
|
||||||
|
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
"bio" TEXT NOT NULL,
|
||||||
|
"avatar" TEXT,
|
||||||
|
"userId" INTEGER NOT NULL,
|
||||||
|
"enabled" BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||||
|
);
|
||||||
|
INSERT INTO "new_Profile" ("avatar", "bio", "enabled", "id", "userId") SELECT "avatar", "bio", "enabled", "id", "userId" FROM "Profile";
|
||||||
|
DROP TABLE "Profile";
|
||||||
|
ALTER TABLE "new_Profile" RENAME TO "Profile";
|
||||||
|
CREATE UNIQUE INDEX "Profile_userId_key" ON "Profile"("userId");
|
||||||
|
PRAGMA foreign_key_check;
|
||||||
|
PRAGMA foreign_keys=ON;
|
||||||
|
|
||||||
|
-- RedefineIndex
|
||||||
|
DROP INDEX "User.email_unique";
|
||||||
|
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
|
@ -4,8 +4,8 @@ import UserRequest from "../Model/UserRequest";
|
|||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
function isProfileExistByEmail(email: string) {
|
async function isProfileExistByEmail(email: string) {
|
||||||
const exist = prisma.profile.count({
|
const exist = await prisma.profile.count({
|
||||||
where: {
|
where: {
|
||||||
user: {
|
user: {
|
||||||
email: email,
|
email: email,
|
||||||
@ -13,7 +13,7 @@ function isProfileExistByEmail(email: string) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return exist.then((c) => c > 0).catch((e) => 0);
|
return exist > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findOneProfileByEmail(email: string) {
|
async function findOneProfileByEmail(email: string) {
|
||||||
@ -31,14 +31,14 @@ async function findOneProfileByEmail(email: string) {
|
|||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isExistByEmail(email: string) {
|
async function isExistByEmail(email: string) {
|
||||||
const exist = prisma.user.count({
|
const exist = await prisma.user.count({
|
||||||
where: {
|
where: {
|
||||||
email: email,
|
email: email,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return exist.then((c) => c > 0).catch((e) => 0);
|
return exist > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findOneByEmail(
|
async function findOneByEmail(
|
||||||
@ -59,7 +59,7 @@ async function findOneByEmail(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createUser(request: UserRequest) {
|
async function createUser(request: UserRequest) {
|
||||||
if (isExistByEmail(request.email)) {
|
if (await isExistByEmail(request.email)) {
|
||||||
return findOneByEmail(request.email);
|
return findOneByEmail(request.email);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ async function createUser(request: UserRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createProfile(user: User, request: ProfileRequest) {
|
async function createProfile(user: User, request: ProfileRequest) {
|
||||||
if (isProfileExistByEmail(user.email)) {
|
if (await isProfileExistByEmail(user.email)) {
|
||||||
return findOneProfileByEmail(user.email);
|
return findOneProfileByEmail(user.email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,13 +15,13 @@ describe("user", function () {
|
|||||||
const bio = "Software Developer";
|
const bio = "Software Developer";
|
||||||
|
|
||||||
// create user test
|
// create user test
|
||||||
it("createUser", async (done) => {
|
it("createUser", (done) => {
|
||||||
// create user
|
// create user
|
||||||
await createUser({
|
createUser({
|
||||||
email: email,
|
email: email,
|
||||||
name: name,
|
name: name,
|
||||||
})
|
})
|
||||||
.then(async (user) => {
|
.then((user) => {
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw Error("user is null");
|
throw Error("user is null");
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ describe("user", function () {
|
|||||||
expect(name).toBe(user.name);
|
expect(name).toBe(user.name);
|
||||||
|
|
||||||
// create profile
|
// create profile
|
||||||
await createProfile(user, {
|
createProfile(user, {
|
||||||
bio: "Software Developer",
|
bio: "Software Developer",
|
||||||
})
|
})
|
||||||
.then((profile) => {
|
.then((profile) => {
|
||||||
@ -50,8 +50,8 @@ describe("user", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// check user test
|
// check user test
|
||||||
it("checkUser", async (done) => {
|
it("checkUser", (done) => {
|
||||||
await findOneByEmail(email)
|
findOneByEmail(email)
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw Error("user is null");
|
throw Error("user is null");
|
||||||
@ -66,8 +66,8 @@ describe("user", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// check profile included user test
|
// check profile included user test
|
||||||
it("checkProfileUser", async (done) => {
|
it("checkProfileUser", (done) => {
|
||||||
await findOneProfileByEmail(email)
|
findOneProfileByEmail(email)
|
||||||
.then((profile) => {
|
.then((profile) => {
|
||||||
if (profile == null) {
|
if (profile == null) {
|
||||||
throw Error("profile is null");
|
throw Error("profile is null");
|
||||||
@ -83,8 +83,8 @@ describe("user", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// try to finish
|
// try to finish
|
||||||
afterAll(async (done) => {
|
afterAll((done) => {
|
||||||
await prisma.$disconnect();
|
prisma.$disconnect();
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user