From 3d686e3f79df9443897908fca14766dfb1606f1d Mon Sep 17 00:00:00 2001 From: Raheel Riaz Date: Thu, 27 Apr 2023 18:32:03 +0500 Subject: [PATCH] fix(nextj-with-roq-auth): CPD-2797 user creation in tenant mode --- .../src/pages/api/chat/assign-tags.ts | 19 +------- .../pages/api/chat/create-1-1-conversation.ts | 19 +------- .../api/chat/create-group-conversation.ts | 43 ++----------------- .../src/pages/api/chat/send-message.ts | 21 +-------- .../src/pages/api/chat/send-system-message.ts | 27 +----------- .../src/pages/api/users/index.ts | 29 +++---------- .../src/server/services/user.service.ts | 39 ++++++++++++++--- 7 files changed, 50 insertions(+), 147 deletions(-) diff --git a/nextjs-with-roq-auth/src/pages/api/chat/assign-tags.ts b/nextjs-with-roq-auth/src/pages/api/chat/assign-tags.ts index eb4ce6f..7674a12 100644 --- a/nextjs-with-roq-auth/src/pages/api/chat/assign-tags.ts +++ b/nextjs-with-roq-auth/src/pages/api/chat/assign-tags.ts @@ -1,8 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { getServerSession, withAuth } from '@roq/nextjs'; -import { faker } from '@faker-js/faker'; import { roqClient } from 'server/roq'; -import { randomUUID } from 'crypto'; +import { UserService } from '../../../server/services/user.service'; async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== 'POST') { @@ -18,21 +17,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { try { // Create a user to chat with - const user = await roqClient.asSuperAdmin().createUser({ - user: { - firstName: "System", - lastName: "Bot", - email: faker.internet.email(), - password: faker.internet.password(), - active: true, - isOptedIn: true, - customData: { - countryCode: "EN", - gender: faker.name.gender(), - }, - reference: randomUUID(), - }, - }); + const user = await UserService.createUser(); // Create the conversation const conv = await roqClient.asUser(session.roqUserId).createConversation({ diff --git a/nextjs-with-roq-auth/src/pages/api/chat/create-1-1-conversation.ts b/nextjs-with-roq-auth/src/pages/api/chat/create-1-1-conversation.ts index 4c4c819..cfe77e9 100644 --- a/nextjs-with-roq-auth/src/pages/api/chat/create-1-1-conversation.ts +++ b/nextjs-with-roq-auth/src/pages/api/chat/create-1-1-conversation.ts @@ -1,8 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { getServerSession, withAuth } from '@roq/nextjs'; -import { faker } from '@faker-js/faker'; import { roqClient } from 'server/roq'; -import { randomUUID } from 'crypto'; +import { UserService } from '../../../server/services/user.service'; async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== 'POST') { @@ -18,21 +17,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { try { // Create a user to chat with - const user = await roqClient.asSuperAdmin().createUser({ - user: { - firstName: faker.name.firstName(), - lastName: faker.name.lastName(), - email: faker.internet.email(), - password: faker.internet.password(), - active: true, - isOptedIn: true, - customData: { - countryCode: "EN", - gender: faker.name.gender(), - }, - reference: randomUUID(), - }, - }); + const user = await UserService.createUser(); // Create the conversation const data = await roqClient.asUser(session.roqUserId).createConversation({ diff --git a/nextjs-with-roq-auth/src/pages/api/chat/create-group-conversation.ts b/nextjs-with-roq-auth/src/pages/api/chat/create-group-conversation.ts index 464cd02..205f09f 100644 --- a/nextjs-with-roq-auth/src/pages/api/chat/create-group-conversation.ts +++ b/nextjs-with-roq-auth/src/pages/api/chat/create-group-conversation.ts @@ -2,8 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { getServerSession, withAuth } from '@roq/nextjs'; import { faker } from '@faker-js/faker'; import { roqClient } from 'server/roq'; -import sampleSize from 'lodash/sampleSize'; -import { randomUUID } from 'crypto'; +import { UserService } from '../../../server/services/user.service'; async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== 'POST') { @@ -15,46 +14,10 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { return res.status(200).json({ success: false }); } try { - const users = sampleSize( - await ( - await roqClient.asSuperAdmin().users({ limit: 10 }) - ).users?.data, - 5 - ); - // Create a user to chat with - const userOne = await roqClient.asSuperAdmin().createUser({ - user: { - firstName: faker.name.firstName(), - lastName: faker.name.lastName(), - email: faker.internet.email(), - password: faker.internet.password(), - active: true, - isOptedIn: true, - customData: { - countryCode: "EN", - gender: faker.name.gender(), - }, - reference: randomUUID(), - }, - }); - + const userOne = await UserService.createUser(); // Create a second user to chat with - const userTwo = await roqClient.asSuperAdmin().createUser({ - user: { - firstName: faker.name.firstName(), - lastName: faker.name.lastName(), - email: faker.internet.email(), - password: faker.internet.password(), - active: true, - isOptedIn: true, - customData: { - countryCode: "EN", - gender: faker.name.gender(), - }, - reference: randomUUID(), - }, - }); + const userTwo = await UserService.createUser(); const { groupName } = JSON.parse(req?.body); // Create the conversation diff --git a/nextjs-with-roq-auth/src/pages/api/chat/send-message.ts b/nextjs-with-roq-auth/src/pages/api/chat/send-message.ts index 7f79a5a..0949ebe 100644 --- a/nextjs-with-roq-auth/src/pages/api/chat/send-message.ts +++ b/nextjs-with-roq-auth/src/pages/api/chat/send-message.ts @@ -1,9 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { getServerSession, withAuth } from '@roq/nextjs'; -import { faker } from '@faker-js/faker'; import { roqClient } from 'server/roq'; -import sampleSize from 'lodash/sampleSize'; -import { randomUUID } from 'crypto'; +import { UserService } from '../../../server/services/user.service'; async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== 'POST') { @@ -22,22 +20,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { let conv const convs = await roqClient.asUser(session.roqUserId).conversations(); if (!convs || convs.conversations?.data?.length === 0) { - const user = await roqClient.asSuperAdmin().createUser({ - user: { - firstName: faker.name.firstName(), - lastName: faker.name.lastName(), - email: faker.internet.email(), - password: faker.internet.password(), - active: true, - isOptedIn: true, - customData: { - countryCode: "EN", - gender: faker.name.gender(), - }, - reference: randomUUID(), - }, - }); - + const user = await UserService.createUser(); // Create the conversation const data = await roqClient.asUser(session.roqUserId).createConversation({ conversation: { diff --git a/nextjs-with-roq-auth/src/pages/api/chat/send-system-message.ts b/nextjs-with-roq-auth/src/pages/api/chat/send-system-message.ts index 04062d9..4f911b8 100644 --- a/nextjs-with-roq-auth/src/pages/api/chat/send-system-message.ts +++ b/nextjs-with-roq-auth/src/pages/api/chat/send-system-message.ts @@ -1,9 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { getServerSession, withAuth } from '@roq/nextjs'; -import { faker } from '@faker-js/faker'; import { roqClient } from 'server/roq'; -import sampleSize from 'lodash/sampleSize'; -import { randomUUID } from 'crypto'; +import { UserService } from '../../../server/services/user.service'; async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== 'POST') { @@ -18,29 +16,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { } try { - const users = sampleSize( - await ( - await roqClient.asSuperAdmin().users({ limit: 10 }) - ).users?.data, - 5 - ); - // Create a user to chat with - const user = await roqClient.asSuperAdmin().createUser({ - user: { - firstName: "System", - lastName: "Bot", - email: faker.internet.email(), - password: faker.internet.password(), - active: true, - isOptedIn: true, - customData: { - countryCode: "EN", - gender: faker.name.gender(), - }, - reference: randomUUID(), - }, - }); + const user = await UserService.createUser(); // Create the conversation const conv = await roqClient.asUser(session.roqUserId).createConversation({ diff --git a/nextjs-with-roq-auth/src/pages/api/users/index.ts b/nextjs-with-roq-auth/src/pages/api/users/index.ts index 7b3132c..91503cd 100644 --- a/nextjs-with-roq-auth/src/pages/api/users/index.ts +++ b/nextjs-with-roq-auth/src/pages/api/users/index.ts @@ -1,30 +1,13 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { withAuth, getServerSession } from "@roq/nextjs"; -import { roqClient } from "server/roq"; -import { faker } from "@faker-js/faker"; -import { randomUUID } from "crypto"; +import type { NextApiRequest, NextApiResponse } from 'next'; +import { withAuth } from '@roq/nextjs'; +import { UserService } from '../../../server/services/user.service'; async function handler(req: NextApiRequest, res: NextApiResponse) { - if (req.method !== "POST") { - res.status(405).send({ message: "Method not allowed" }); + if (req.method !== 'POST') { + res.status(405).send({ message: 'Method not allowed' }); res.end(); } - - const user = await roqClient.asSuperAdmin().createUser({ - user: { - firstName: faker.name.firstName(), - lastName: faker.name.lastName(), - email: faker.internet.email(), - password: faker.internet.password(), - active: true, - isOptedIn: true, - customData: { - countryCode: "EN", - gender: faker.name.gender(), - }, - reference: randomUUID(), - }, - }); + const user = await UserService.createUser(); res.status(200).json({ user: user.createUser }); } diff --git a/nextjs-with-roq-auth/src/server/services/user.service.ts b/nextjs-with-roq-auth/src/server/services/user.service.ts index b3c7788..27fc413 100644 --- a/nextjs-with-roq-auth/src/server/services/user.service.ts +++ b/nextjs-with-roq-auth/src/server/services/user.service.ts @@ -1,14 +1,17 @@ -import { NotificationTypes } from "server/enums"; -import { roqClient } from "server/roq"; -import { PrismaClient } from "@prisma/client"; +import { NotificationTypes } from 'server/enums'; +import { roqClient } from 'server/roq'; +import { PrismaClient } from '@prisma/client'; +import { faker } from '@faker-js/faker'; +import { randomUUID } from 'crypto'; +import { UserCreateDto } from '@roq/nodejs/dist/src/generated/sdk'; const prisma = new PrismaClient(); export class UserService { static async syncUser( - email: string, - roqUserId: string, - type: string = "user" + email: string, + roqUserId: string, + type: string = 'user' ) { await prisma.user.upsert({ where: { @@ -49,4 +52,28 @@ export class UserService { }, }); } + + static async createUser(createUser?: UserCreateDto) { + const isTenantActive = await roqClient.asSuperAdmin().isTenantActive(); + const tenantId: string | undefined = isTenantActive ? (await roqClient.asSuperAdmin().tenants({ + limit: 1, + }))?.tenants?.data?.[0]?.id : undefined; + const user = createUser ? { ...createUser, tenantId } : { + firstName: faker.name.firstName(), + lastName: faker.name.lastName(), + email: faker.internet.email(), + password: faker.internet.password(), + active: true, + isOptedIn: true, + customData: { + countryCode: 'EN', + gender: faker.name.gender(), + }, + reference: randomUUID(), + tenantId, + }; + return roqClient.asSuperAdmin().createUser({ + user + }) + } }