Skip to content

Commit

Permalink
rename model
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansbk committed Dec 19, 2023
1 parent c001a58 commit e77ea91
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ jobs:
tags: usmansbk/userbase-api-server-template:${{ github.ref_name }}

- name: Run migrations
run: yarn db:deploy
run: yarn db:migrate:reset
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
2 changes: 1 addition & 1 deletion assets/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"message_P2025": "User role does not exists."
}
},
"deleteUserSessions": {
"deleteSessions": {
"errors": {
"message": "Failed to delete user sessions.",
"message_P2025": "User session does not exists."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ CREATE TABLE "Permission" (
);

-- CreateTable
CREATE TABLE "UserSession" (
CREATE TABLE "Session" (
"id" TEXT NOT NULL,
"jti" TEXT NOT NULL,
"clientId" TEXT NOT NULL,
Expand All @@ -55,7 +55,7 @@ CREATE TABLE "UserSession" (
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"ownerId" TEXT NOT NULL,

CONSTRAINT "UserSession_pkey" PRIMARY KEY ("id")
CONSTRAINT "Session_pkey" PRIMARY KEY ("id")
);

-- CreateTable
Expand Down Expand Up @@ -181,7 +181,7 @@ ALTER TABLE "Role" ADD CONSTRAINT "Role_creatorId_fkey" FOREIGN KEY ("creatorId"
ALTER TABLE "Permission" ADD CONSTRAINT "Permission_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserSession" ADD CONSTRAINT "UserSession_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "Session" ADD CONSTRAINT "Session_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserAvatar" ADD CONSTRAINT "UserAvatar_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Expand Down
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enum UserStatus {
Deprovisioned
}

model UserSession {
model Session {
id String @id @default(cuid())
jti String @default(cuid())
clientId String
Expand Down Expand Up @@ -101,7 +101,7 @@ model User {
permissionsAssignedToUser UserPermission[] @relation("PermissionAssignee")
permissionsAssignedByUser UserPermission[] @relation("PermissionAssignor")
rolePermissionsAssignedByUser RolePermission[] @relation("RolePermissionAssignor")
sessions UserSession[]
sessions Session[]
}

model UserAvatar {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/createPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ const permissions: Array<{ name: string; description: string }> = [
description: "Enables the deletion of an existing user avatar.",
},
{
name: "ReadUserSession",
name: "ReadSession",
description:
"Allows access to view details and information about user sessions.",
},
{
name: "DeleteUserSession",
name: "DeleteSession",
description: "Enables the termination or deletion of user sessions.",
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { MutationDeleteUserSessionsArgs } from "types/graphql";
import type { MutationDeleteSessionsArgs } from "types/graphql";
import type { AppContext } from "types";
import type { UserSession } from "@prisma/client";
import type { Session } from "@prisma/client";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import QueryError from "@/utils/errors/QueryError";

export default {
Mutation: {
async deleteUserSessions(
async deleteSessions(
_parent: unknown,
{ inputs }: MutationDeleteUserSessionsArgs,
{ inputs }: MutationDeleteSessionsArgs,
context: AppContext,
): Promise<UserSession[]> {
): Promise<Session[]> {
const { prismaClient, t } = context;

try {
return await prismaClient.$transaction(
inputs.map(({ id }) =>
prismaClient.userSession.delete({
prismaClient.session.delete({
where: {
id,
},
Expand All @@ -26,7 +26,7 @@ export default {
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
throw new QueryError(
t("mutation.deleteUserSessions.errors.message", {
t("mutation.deleteSessions.errors.message", {
context: e.code as unknown,
count: inputs.length,
meta: e.meta,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { QuerySessionArgs, QuerySessionsArgs } from "types/graphql";
import type { AppContext } from "types";
import type { UserSession } from "@prisma/client";
import type { Session } from "@prisma/client";
import QueryError from "@/utils/errors/QueryError";
import { DEFAULT_LIST_SIZE } from "@/constants/limits";

Expand All @@ -10,10 +10,10 @@ export default {
_parent: unknown,
{ id }: QuerySessionArgs,
context: AppContext,
): Promise<UserSession> {
): Promise<Session> {
const { prismaClient, t } = context;

const session = await prismaClient.userSession.findUnique({
const session = await prismaClient.session.findUnique({
where: {
id,
},
Expand All @@ -32,7 +32,7 @@ export default {
) {
const { prismaClient } = context;

const items = await prismaClient.userSession.findMany({
const items = await prismaClient.session.findMany({
take: limit ?? DEFAULT_LIST_SIZE,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { UserSession } from "@prisma/client";
import type { Session } from "@prisma/client";
import type { AppContext } from "types";

export default {
UserSession: {
async user(session: UserSession, _args: never, context: AppContext) {
Session: {
async user(session: Session, _args: never, context: AppContext) {
const { prismaClient } = context;

return await prismaClient.userSession
return await prismaClient.session
.findUnique({
where: {
id: session.id,
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/resolvers/User/Mutation/loginWithEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default {
);
}

const session = await prismaClient.userSession.create({
const session = await prismaClient.session.create({
data: {
clientId,
clientIp,
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/resolvers/User/Mutation/loginWithEmailOTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default {

await redisClient.del(cacheKey);

const session = await prismaClient.userSession.create({
const session = await prismaClient.session.create({
data: {
clientId,
clientIp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default {
});
}

const session = await prismaClient.userSession.create({
const session = await prismaClient.session.create({
data: {
clientId,
clientIp,
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/resolvers/User/Mutation/loginWithSMSOTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default {

await redisClient.del(cacheKey);

const session = await prismaClient.userSession.create({
const session = await prismaClient.session.create({
data: {
clientId,
clientIp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
): Promise<MutationResponse> {
const { prismaClient, currentUser, t } = context;

await prismaClient.userSession.deleteMany({
await prismaClient.session.deleteMany({
where: {
id: {
in: currentUser!.sessions.map((session) => session.id),
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/resolvers/User/Mutation/registerWithEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default {
});
}

const session = await prismaClient.userSession.create({
const session = await prismaClient.session.create({
data: {
clientId,
clientIp,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type UserSession
type Session
@auth(
rules: [
{ allow: owner }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserSession"] }
{ allow: permissions, permissions: ["ReadSession"] }
]
) {
id: ID!
Expand All @@ -16,37 +16,37 @@ type UserSession
}

type Query {
session(id: ID!): UserSession!
session(id: ID!): Session!
@auth(
rules: [
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserSession"] }
{ allow: permissions, permissions: ["ReadSession"] }
]
)

sessions(limit: Int): SessionsList!
@auth(
rules: [
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserSession"] }
{ allow: permissions, permissions: ["ReadSession"] }
]
)
}

type Mutation {
deleteUserSessions(inputs: [DeleteUserSessionInput!]!): [UserSession]!
deleteSessions(inputs: [DeleteSessionInput!]!): [Session]!
@auth(
rules: [
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["DeleteUserSession"] }
{ allow: permissions, permissions: ["DeleteSession"] }
]
)
}

type SessionsList {
items: [UserSession]!
items: [Session]!
}

input DeleteUserSessionInput {
input DeleteSessionInput {
id: ID!
}
61 changes: 52 additions & 9 deletions src/graphql/typeDefs/User.gql
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,77 @@ type User {
passwordUpdatedAt: DateTime
roles: [String]!
status: AccountStatus
sessions: [UserSession]!
sessions: [Session]!
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserSession"] }
{ allow: permissions, permissions: ["ReadSession"] }
]
)

rolesCreatedByUser: [Role]! @auth(rules: [{ allow: roles, roles: ["Admin"] }])
rolesCreatedByUser: [Role]!
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadRole"] }
]
)

permissionsCreatedByUser: [Permission]!
@auth(rules: [{ allow: roles, roles: ["Admin"] }])
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadPermission"] }
]
)

rolesAssignedToUser: [UserRole]!
@auth(rules: [{ allow: roles, roles: ["Admin"] }])
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserRole"] }
]
)

rolesAssignedByUser: [UserRole]!
@auth(rules: [{ allow: roles, roles: ["Admin"] }])
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserRole"] }
]
)

permissionsAssignedToUser: [UserPermission]!
@auth(rules: [{ allow: roles, roles: ["Admin"] }])
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserPermission"] }
]
)

permissionsAssignedByUser: [UserPermission]!
@auth(rules: [{ allow: roles, roles: ["Admin"] }])
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadUserPermission"] }
]
)

rolePermissionsAssignedByUser: [RolePermission]!
@auth(rules: [{ allow: roles, roles: ["Admin"] }])
@auth(
rules: [
{ allow: owner, ownerField: "id" }
{ allow: roles, roles: ["Admin"] }
{ allow: permissions, permissions: ["ReadRolePermission"] }
]
)
}

type Query {
Expand Down
4 changes: 2 additions & 2 deletions src/v1/controllers/auth/refreshToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ export default function refreshToken(

const [oldSession] = user.sessions;

await prismaClient.userSession.delete({
await prismaClient.session.delete({
where: {
id: oldSession.id,
},
});

const session = await prismaClient.userSession.create({
const session = await prismaClient.session.create({
data: {
clientId,
clientIp,
Expand Down
Loading

0 comments on commit e77ea91

Please sign in to comment.