This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add members mutation & query
- Loading branch information
1 parent
e0a12d0
commit 58b3396
Showing
7 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/server/prisma/migrations/20210206121640_add_tenant_data/migration.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
-- CreateEnum | ||
CREATE TYPE "Role" AS ENUM ('OWNER', 'MANAGER', 'WAITER'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Tenant" ( | ||
"id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"domain_name" TEXT NOT NULL, | ||
"hotline" TEXT NOT NULL, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "TenantUser" ( | ||
"role" "Role" NOT NULL DEFAULT E'OWNER', | ||
"userId" INTEGER NOT NULL, | ||
"tenantId" INTEGER NOT NULL, | ||
|
||
PRIMARY KEY ("userId","tenantId") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Tenant.domain_name_unique" ON "Tenant"("domain_name"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TenantUser" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TenantUser" ADD FOREIGN KEY ("tenantId") REFERENCES "Tenant"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains 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
7 changes: 7 additions & 0 deletions
7
packages/server/src/graphql/member/errors/NonExistingMemberError.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ObjectType } from "type-graphql"; | ||
import { IError } from "../../common/errors/IError"; | ||
|
||
@ObjectType({ implements: IError }) | ||
export class NonExistingMemberError implements IError { | ||
message: string = "member doesn't exist"; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Field, InputType, Int } from "type-graphql"; | ||
import { IsNotEmpty } from "class-validator"; | ||
|
||
@InputType() | ||
export class MemberInput { | ||
@IsNotEmpty({ message: "required field" }) | ||
@Field() | ||
userId: number; | ||
|
||
@IsNotEmpty({ message: "required field" }) | ||
@Field() | ||
tenantId: number; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Arg, Ctx, Mutation, Query, Resolver } from "type-graphql"; | ||
import { Context } from "../../context"; | ||
import { MemberInput } from "./inputs/memberInput"; | ||
import { UpdateMemberResult } from "./results/UpdateMemberResult"; | ||
import { DeleteMemberResult } from "./results/DeleteMemberResult"; | ||
import { validate } from "class-validator"; | ||
import { FieldsValidationError } from "../common/errors/FieldsValidationError"; | ||
import { NonExistingMemberError } from "./errors/NonExistingMemberError"; | ||
import { TenantUser } from "../../generated/typegraphql-prisma/models/TenantUser"; | ||
import { Role } from "../../generated/typegraphql-prisma/enums/Role"; | ||
|
||
@Resolver() | ||
class MemberResolver { | ||
@Query(() => [TenantUser], { nullable: true }) | ||
async getMembers(@Ctx() ctx: Context): Promise<TenantUser[] | null> { | ||
return await ctx.prisma.tenantUser.findMany({ | ||
include: { | ||
User: true, | ||
Tenant: true, | ||
}, | ||
}); | ||
} | ||
|
||
@Mutation(() => DeleteMemberResult) | ||
async deleteMember( | ||
@Arg("input", () => MemberInput) input: MemberInput, | ||
@Ctx() ctx: Context | ||
): Promise<typeof DeleteMemberResult> { | ||
const errors = await validate(input); | ||
if (errors.length > 0) return FieldsValidationError.from(errors); | ||
|
||
await ctx.prisma.tenantUser.delete({ | ||
where: { | ||
userId_tenantId: input, | ||
}, | ||
}); | ||
} | ||
|
||
@Mutation(() => UpdateMemberResult) | ||
async updateMember( | ||
@Arg("input") input: MemberInput, | ||
@Arg("role", () => Role) role: Role, | ||
@Ctx() ctx: Context | ||
): Promise<typeof UpdateMemberResult> { | ||
const errors = await validate(input); | ||
if (errors.length > 0) return FieldsValidationError.from(errors); | ||
|
||
try { | ||
const updateMember = await ctx.prisma.tenantUser.update({ | ||
where: { | ||
userId_tenantId: input, | ||
}, | ||
data: { | ||
role: role, | ||
}, | ||
}); | ||
return Object.assign(new TenantUser(), updateMember); | ||
} catch (err) { | ||
return new NonExistingMemberError(); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/server/src/graphql/member/results/DeleteMemberResult.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createUnionType } from "type-graphql"; | ||
import { FieldsValidationError } from "../../common/errors/FieldsValidationError"; | ||
|
||
export const DeleteMemberResult = createUnionType({ | ||
name: "DeleteMemberResult", | ||
types: () => [FieldsValidationError] as const, | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/server/src/graphql/member/results/UpdateMemberResult.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createUnionType } from "type-graphql"; | ||
import { TenantUser } from "../../../generated/typegraphql-prisma/models/TenantUser"; | ||
import { FieldsValidationError } from "../../common/errors/FieldsValidationError"; | ||
import { NonExistingMemberError } from "../errors/NonExistingMemberError"; | ||
|
||
export const UpdateMemberResult = createUnionType({ | ||
name: "UpdateMemberResult", | ||
types: () => | ||
[TenantUser, FieldsValidationError, NonExistingMemberError] as const, | ||
}); |