From 3f3ccf147fcd55202565ad7c85cab1c89666aea2 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Tue, 4 Aug 2020 13:12:25 +0530 Subject: [PATCH] :recycle: Use number for ID, not string --- src/_staart/helpers/authorization.ts | 8 +- src/_staart/helpers/webhooks.ts | 2 +- src/_staart/rest/auth.ts | 10 +- src/_staart/rest/group.ts | 213 +++++++++++++------------- src/_staart/rest/user.ts | 189 +++++++++++------------ src/_staart/services/group.service.ts | 4 +- 6 files changed, 208 insertions(+), 218 deletions(-) diff --git a/src/_staart/helpers/authorization.ts b/src/_staart/helpers/authorization.ts index dc1919a4d..db37cd0bf 100644 --- a/src/_staart/helpers/authorization.ts +++ b/src/_staart/helpers/authorization.ts @@ -165,16 +165,16 @@ const canApiKeyGroup = (apiKey: apiKeys, action: OrgScopes, target: groups) => { * Whether a user has authorization to perform an action */ export const can = async ( - user: string | users | ApiKeyResponse | AccessTokenResponse, + user: number | users | ApiKeyResponse | AccessTokenResponse, action: OrgScopes | UserScopes | SudoScopes, targetType: "user" | "group" | "membership" | "sudo", - target?: string | users | groups | memberships + target?: number | users | groups | memberships ) => { let requestFromType: "users" | "apiKeys" | "accessTokens" = "users"; /** * First, we figure out what the first parameter is - * If it's a string, it can only be a user ID we'll convert to user + * If it's a number, it can only be a user ID we'll convert to user */ if (typeof user === "object") { if ((user as ApiKeyResponse).sub === Tokens.API_KEY) { @@ -193,7 +193,7 @@ export const can = async ( * and `requestFromType` will tell us what type it is * We find what the correct target is */ - if (typeof target === "string") { + if (typeof target === "number") { if (targetType === "membership") { const membership = await prisma.memberships.findOne({ where: { id: parseInt(target) }, diff --git a/src/_staart/helpers/webhooks.ts b/src/_staart/helpers/webhooks.ts index 03cbb6e25..65ecb9ee0 100644 --- a/src/_staart/helpers/webhooks.ts +++ b/src/_staart/helpers/webhooks.ts @@ -19,7 +19,7 @@ const setupQueue = async () => { }; export const queueWebhook = ( - groupId: string, + groupId: number, webhook: Webhooks, data?: any ) => { diff --git a/src/_staart/rest/auth.ts b/src/_staart/rest/auth.ts index b43c2578f..008ef74f5 100644 --- a/src/_staart/rest/auth.ts +++ b/src/_staart/rest/auth.ts @@ -121,7 +121,7 @@ export const register = async ( user: usersCreateInput, locals?: Locals, email?: string, - groupId?: string, + groupId?: number, role?: MembershipRole, emailVerified = false ) => { @@ -137,7 +137,7 @@ export const register = async ( try { domain = email.split("@")[1]; const domainDetails = await getDomainByDomainName(domain); - groupId = domainDetails.groupId.toString(); + groupId = domainDetails.groupId; } catch (error) {} } const userId = ( @@ -148,7 +148,7 @@ export const register = async ( memberships: { create: { group: { - connect: { id: parseInt(groupId) }, + connect: { id: groupId }, }, role, }, @@ -254,8 +254,8 @@ export const updatePassword = async ( }; export const impersonate = async ( - tokenUserId: string, - impersonateUserId: string, + tokenUserId: number, + impersonateUserId: number, locals: Locals ) => { if ( diff --git a/src/_staart/rest/group.ts b/src/_staart/rest/group.ts index de5adfa0e..ccf46f01f 100644 --- a/src/_staart/rest/group.ts +++ b/src/_staart/rest/group.ts @@ -80,8 +80,8 @@ import { getUserById } from "../services/user.service"; import { register } from "./auth"; export const getGroupForUser = async ( - userId: string | ApiKeyResponse, - groupId: string + userId: number | ApiKeyResponse, + groupId: number ) => { if (await can(userId, OrgScopes.READ_ORG, "group", groupId)) return getGroupById(groupId); @@ -89,7 +89,7 @@ export const getGroupForUser = async ( }; export const newGroupForUser = async ( - userId: string, + userId: number, group: groupsCreateInput, locals: Locals ) => { @@ -101,15 +101,15 @@ export const newGroupForUser = async ( }; export const updateGroupForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, data: groupsUpdateInput, locals: Locals ) => { if (await can(userId, OrgScopes.UPDATE_ORG, "group", groupId)) { const result = await prisma.groups.update({ where: { - id: parseInt(groupId), + id: groupId, }, data, }); @@ -121,8 +121,8 @@ export const updateGroupForUser = async ( }; export const deleteGroupForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, locals: Locals ) => { if (await can(userId, OrgScopes.DELETE_ORG, "group", groupId)) { @@ -136,7 +136,7 @@ export const deleteGroupForUser = async ( await deleteCustomer(groupDetails.attributes?.stripeCustomerId); await prisma.groups.delete({ where: { - id: parseInt(groupId), + id: groupId, }, }); queueWebhook(groupId, Webhooks.DELETE_ORGANIZATION); @@ -147,8 +147,8 @@ export const deleteGroupForUser = async ( }; export const getGroupBillingForUser = async ( - userId: string | ApiKeyResponse, - groupId: string + userId: number | ApiKeyResponse, + groupId: number ) => { if (await can(userId, OrgScopes.READ_ORG_BILLING, "group", groupId)) { const group = await getGroupById(groupId); @@ -165,8 +165,8 @@ export const getGroupBillingForUser = async ( }; export const updateGroupBillingForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, data: any, locals: Locals ) => { @@ -184,13 +184,8 @@ export const updateGroupBillingForUser = async ( result = await createCustomer( groupId, data, - (groupId: string, data: groupsUpdateInput) => - prisma.groups.update({ - where: { - id: parseInt(groupId), - }, - data, - }) + (groupId: number, data: groupsUpdateInput) => + prisma.groups.update({ where: { id: groupId }, data }) ); } queueWebhook(groupId, Webhooks.UPDATE_ORGANIZATION_BILLING, data); @@ -201,8 +196,8 @@ export const updateGroupBillingForUser = async ( }; export const getGroupInvoicesForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, params: KeyValue ) => { if (await can(userId, OrgScopes.READ_ORG_INVOICES, "group", groupId)) { @@ -220,8 +215,8 @@ export const getGroupInvoicesForUser = async ( }; export const getGroupInvoiceForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, invoiceId: string ) => { if (await can(userId, OrgScopes.READ_ORG_INVOICES, "group", groupId)) { @@ -239,8 +234,8 @@ export const getGroupInvoiceForUser = async ( }; export const getGroupSourcesForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, params: KeyValue ) => { if (await can(userId, OrgScopes.READ_ORG_SOURCES, "group", groupId)) { @@ -258,8 +253,8 @@ export const getGroupSourcesForUser = async ( }; export const getGroupSourceForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, sourceId: string ) => { if (await can(userId, OrgScopes.READ_ORG_SOURCES, "group", groupId)) { @@ -277,8 +272,8 @@ export const getGroupSourceForUser = async ( }; export const getGroupSubscriptionsForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, params: KeyValue ) => { if (await can(userId, OrgScopes.READ_ORG_SUBSCRIPTIONS, "group", groupId)) { @@ -296,8 +291,8 @@ export const getGroupSubscriptionsForUser = async ( }; export const getGroupSubscriptionForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, subscriptionId: string ) => { if (await can(userId, OrgScopes.READ_ORG_SUBSCRIPTIONS, "group", groupId)) { @@ -318,8 +313,8 @@ export const getGroupSubscriptionForUser = async ( }; export const updateGroupSubscriptionForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, subscriptionId: string, data: KeyValue, locals?: Locals @@ -350,8 +345,8 @@ export const updateGroupSubscriptionForUser = async ( }; export const createGroupSubscriptionForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, params: { plan: string; [index: string]: any }, locals?: Locals ) => { @@ -380,8 +375,8 @@ export const createGroupSubscriptionForUser = async ( }; export const getGroupPricingPlansForUser = async ( - userId: string | ApiKeyResponse, - groupId: string + userId: number | ApiKeyResponse, + groupId: number ) => { if (await can(userId, OrgScopes.READ_ORG_PLANS, "group", groupId)) return getProductPricing(); @@ -389,8 +384,8 @@ export const getGroupPricingPlansForUser = async ( }; export const deleteGroupSourceForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, sourceId: string, locals?: Locals ) => { @@ -419,8 +414,8 @@ export const deleteGroupSourceForUser = async ( }; export const updateGroupSourceForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, sourceId: string, data: any, locals?: Locals @@ -451,8 +446,8 @@ export const updateGroupSourceForUser = async ( }; export const createGroupSourceForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, card: any, locals?: Locals ) => { @@ -481,13 +476,13 @@ export const createGroupSourceForUser = async ( }; export const getAllGroupDataForUser = async ( - userId: string | ApiKeyResponse, - groupId: string + userId: number | ApiKeyResponse, + groupId: number ) => { if (await can(userId, OrgScopes.READ_ORG_TRANSACTIONS, "group", groupId)) { const group = await prisma.groups.findOne({ where: { - id: parseInt(groupId), + id: groupId, }, include: { apiKeys: true, @@ -518,14 +513,14 @@ export const getAllGroupDataForUser = async ( }; export const getGroupMembershipsForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, queryParams: any ) => { if (await can(userId, OrgScopes.READ_ORG_MEMBERSHIPS, "group", groupId)) return paginatedResult( await prisma.memberships.findMany({ - where: { groupId: parseInt(groupId) }, + where: { groupId: groupId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -534,8 +529,8 @@ export const getGroupMembershipsForUser = async ( }; export const getGroupMembershipForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, membershipId: string ) => { if (await can(userId, OrgScopes.READ_ORG_MEMBERSHIPS, "group", groupId)) @@ -547,8 +542,8 @@ export const getGroupMembershipForUser = async ( }; export const updateGroupMembershipForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, membershipId: string, data: membershipsUpdateInput ) => { @@ -560,7 +555,7 @@ export const updateGroupMembershipForUser = async ( if (!currentMembership) throw new Error(MEMBERSHIP_NOT_FOUND); if (currentMembership.role === "OWNER" && data.role !== "OWNER") { const members = await prisma.memberships.findMany({ - where: { groupId: parseInt(groupId), role: "OWNER" }, + where: { groupId: groupId, role: "OWNER" }, }); if (members.length === 1) throw new Error(CANNOT_DELETE_SOLE_MEMBER); } @@ -579,14 +574,14 @@ export const updateGroupMembershipForUser = async ( * Delete the entire group, not just the membership */ export const deleteGroupMembershipForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, membershipId: string, locals: Locals ) => { if (await can(userId, OrgScopes.DELETE_ORG_MEMBERSHIPS, "group", groupId)) { const members = await prisma.memberships.findMany({ - where: { groupId: parseInt(groupId) }, + where: { groupId: groupId }, }); if (members.length === 1) return deleteGroupForUser(userId, groupId, locals); @@ -596,8 +591,8 @@ export const deleteGroupMembershipForUser = async ( }; export const inviteMemberToGroup = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, newMemberName: string, newMemberEmail: string, role: MembershipRole, @@ -610,7 +605,7 @@ export const inviteMemberToGroup = async ( const emailDomain = newMemberEmail.split("@")[1]; try { const domainDetails = await getDomainByDomainName(emailDomain); - if (domainDetails.groupId !== parseInt(groupId)) throw new Error(); + if (domainDetails.groupId !== groupId) throw new Error(); } catch (error) { throw new Error(CANNOT_INVITE_DOMAIN); } @@ -634,7 +629,7 @@ export const inviteMemberToGroup = async ( await prisma.memberships.findMany({ where: { userId: newUser.id, - groupId: parseInt(groupId), + groupId: groupId, }, }) ).length !== 0; @@ -643,7 +638,7 @@ export const inviteMemberToGroup = async ( await prisma.memberships.create({ data: { user: { connect: { id: newUser.id } }, - group: { connect: { id: parseInt(groupId) } }, + group: { connect: { id: groupId } }, role, }, }); @@ -684,14 +679,14 @@ export const inviteMemberToGroup = async ( }; export const getGroupApiKeysForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, queryParams: any ) => { if (await can(userId, OrgScopes.READ_ORG_API_KEYS, "group", groupId)) return paginatedResult( await prisma.apiKeys.findMany({ - where: { groupId: parseInt(groupId) }, + where: { groupId: groupId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -700,8 +695,8 @@ export const getGroupApiKeysForUser = async ( }; export const getGroupApiKeyForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, apiKeyId: string ) => { if (await can(userId, OrgScopes.READ_ORG_API_KEYS, "group", groupId)) @@ -710,8 +705,8 @@ export const getGroupApiKeyForUser = async ( }; export const getGroupApiKeyLogsForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, apiKeyId: string, query: { range?: string; @@ -724,8 +719,8 @@ export const getGroupApiKeyLogsForUser = async ( }; export const updateApiKeyForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, apiKeyId: string, data: apiKeysUpdateInput, locals: Locals @@ -743,8 +738,8 @@ export const updateApiKeyForUser = async ( }; export const createApiKeyForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, apiKey: apiKeysCreateInput, locals: Locals ) => { @@ -756,7 +751,7 @@ export const createApiKeyForUser = async ( ...apiKey, group: { connect: { - id: parseInt(groupId), + id: groupId, }, }, }, @@ -769,8 +764,8 @@ export const createApiKeyForUser = async ( }; export const deleteApiKeyForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, apiKeyId: string, locals: Locals ) => { @@ -786,14 +781,14 @@ export const deleteApiKeyForUser = async ( }; export const getGroupDomainsForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, queryParams: any ) => { if (await can(userId, OrgScopes.READ_ORG_DOMAINS, "group", groupId)) return paginatedResult( await prisma.domains.findMany({ - where: { groupId: parseInt(groupId) }, + where: { groupId: groupId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -802,8 +797,8 @@ export const getGroupDomainsForUser = async ( }; export const getGroupDomainForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, domainId: string ) => { if (await can(userId, OrgScopes.READ_ORG_DOMAINS, "group", groupId)) @@ -812,8 +807,8 @@ export const getGroupDomainForUser = async ( }; export const updateDomainForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, domainId: string, data: domainsUpdateInput, locals: Locals @@ -831,8 +826,8 @@ export const updateDomainForUser = async ( }; export const createDomainForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, domain: domainsCreateInput, locals: Locals ) => { @@ -845,7 +840,7 @@ export const createDomainForUser = async ( isVerified: false, group: { connect: { - id: parseInt(groupId), + id: groupId, }, }, }, @@ -858,8 +853,8 @@ export const createDomainForUser = async ( }; export const deleteDomainForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, domainId: string, locals: Locals ) => { @@ -875,8 +870,8 @@ export const deleteDomainForUser = async ( }; export const verifyDomainForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, domainId: string, method: "dns" | "file", locals: Locals @@ -933,14 +928,14 @@ export const verifyDomainForUser = async ( }; export const getGroupWebhooksForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, queryParams: any ) => { if (await can(userId, OrgScopes.READ_ORG_WEBHOOKS, "group", groupId)) return paginatedResult( await prisma.webhooks.findMany({ - where: { groupId: parseInt(groupId) }, + where: { groupId: groupId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -949,8 +944,8 @@ export const getGroupWebhooksForUser = async ( }; export const getGroupWebhookForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, webhookId: string ) => { if (await can(userId, OrgScopes.READ_ORG_WEBHOOKS, "group", groupId)) @@ -959,8 +954,8 @@ export const getGroupWebhookForUser = async ( }; export const updateWebhookForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, webhookId: string, data: webhooksUpdateInput, locals: Locals @@ -978,8 +973,8 @@ export const updateWebhookForUser = async ( }; export const createWebhookForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, webhook: webhooksCreateInput, locals: Locals ) => { @@ -989,7 +984,7 @@ export const createWebhookForUser = async ( ...webhook, group: { connect: { - id: parseInt(groupId), + id: groupId, }, }, }, @@ -1005,8 +1000,8 @@ export const createWebhookForUser = async ( }; export const deleteWebhookForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, webhookId: string, locals: Locals ) => { @@ -1022,8 +1017,8 @@ export const deleteWebhookForUser = async ( }; export const applyCouponToGroupForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, coupon: string ) => { if (await can(userId, OrgScopes.CREATE_ORG_TRANSACTIONS, "group", groupId)) { @@ -1069,8 +1064,8 @@ export const applyCouponToGroupForUser = async ( }; export const getGroupTransactionsForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, params: KeyValue ) => { if (await can(userId, OrgScopes.READ_ORG_TRANSACTIONS, "group", groupId)) { @@ -1091,8 +1086,8 @@ export const getGroupTransactionsForUser = async ( }; export const getGroupTransactionForUser = async ( - userId: string | ApiKeyResponse, - groupId: string, + userId: number | ApiKeyResponse, + groupId: number, transactionId: string ) => { if (await can(userId, OrgScopes.READ_ORG_TRANSACTIONS, "group", groupId)) { diff --git a/src/_staart/rest/user.ts b/src/_staart/rest/user.ts index a253d3871..bac9b82d1 100644 --- a/src/_staart/rest/user.ts +++ b/src/_staart/rest/user.ts @@ -50,14 +50,14 @@ import { import { deleteGroupForUser } from "./group"; export const getUserFromIdForUser = async ( - userId: string, - tokenUserId: string, + userId: number, + tokenUserId: number, queryParams: any ) => { if (await can(tokenUserId, UserScopes.READ_USER, "user", userId)) { const user = await prisma.users.findOne({ ...queryParamsToSelect(queryParams), - where: { id: parseInt(userId) }, + where: { id: userId }, }); if (user) return user; throw new Error(USER_NOT_FOUND); @@ -66,8 +66,8 @@ export const getUserFromIdForUser = async ( }; export const updateUserForUser = async ( - tokenUserId: string, - updateUserId: string, + tokenUserId: number, + updateUserId: number, data: users, locals: Locals ) => { @@ -75,7 +75,7 @@ export const updateUserForUser = async ( if (await can(tokenUserId, UserScopes.UPDATE_USER, "user", updateUserId)) { const user = await prisma.users.update({ data, - where: { id: parseInt(updateUserId) }, + where: { id: updateUserId }, }); await deleteItemFromCache(`cache_getUserById_${updateUserId}`); trackEvent( @@ -92,8 +92,8 @@ export const updateUserForUser = async ( }; export const updatePasswordForUser = async ( - tokenUserId: string, - updateUserId: string, + tokenUserId: number, + updateUserId: number, oldPassword: string, newPassword: string, locals: Locals @@ -108,7 +108,7 @@ export const updatePasswordForUser = async ( } const result = await prisma.users.update({ data: { password: await hash(newPassword, 8) }, - where: { id: parseInt(updateUserId) }, + where: { id: updateUserId }, }); await deleteItemFromCache(`cache_getUserById_${updateUserId}`); trackEvent( @@ -125,8 +125,8 @@ export const updatePasswordForUser = async ( }; export const deleteUserForUser = async ( - tokenUserId: string, - updateUserId: string, + tokenUserId: number, + updateUserId: number, locals: Locals ) => { if (await can(tokenUserId, UserScopes.DELETE_USER, "user", updateUserId)) { @@ -136,7 +136,7 @@ export const deleteUserForUser = async ( }, where: { memberships: { - every: { userId: parseInt(updateUserId) }, + every: { userId: updateUserId }, }, }, }); @@ -151,21 +151,21 @@ export const deleteUserForUser = async ( await prisma.groups.deleteMany({ where: { memberships: { - every: { userId: parseInt(updateUserId) }, + every: { userId: updateUserId }, }, }, }); await prisma.emails.deleteMany({ - where: { userId: parseInt(updateUserId) }, + where: { userId: updateUserId }, }); await prisma.memberships.deleteMany({ - where: { userId: parseInt(updateUserId) }, + where: { userId: updateUserId }, }); await prisma.approvedLocations.deleteMany({ - where: { userId: parseInt(updateUserId) }, + where: { userId: updateUserId }, }); const originalUser = await getUserById(updateUserId); - await prisma.users.delete({ where: { id: parseInt(updateUserId) } }); + await prisma.users.delete({ where: { id: updateUserId } }); await deleteItemFromCache(`cache_getUserById_${originalUser.id}`); trackEvent( { @@ -181,8 +181,8 @@ export const deleteUserForUser = async ( }; export const getMembershipsForUser = async ( - tokenUserId: string, - dataUserId: string, + tokenUserId: number, + dataUserId: number, queryParams: any ) => { if ( @@ -191,7 +191,7 @@ export const getMembershipsForUser = async ( return paginatedResult( await prisma.memberships.findMany({ ...queryParamsToSelect(queryParams), - where: { userId: parseInt(dataUserId) }, + where: { userId: dataUserId }, include: { group: true }, }), { first: queryParams.first, last: queryParams.last } @@ -200,13 +200,13 @@ export const getMembershipsForUser = async ( }; export const getAllDataForUser = async ( - tokenUserId: string, - userId: string + tokenUserId: number, + userId: number ) => { if (!(await can(tokenUserId, UserScopes.READ_USER, "user", userId))) throw new Error(INSUFFICIENT_PERMISSION); return prisma.users.findOne({ - where: { id: parseInt(userId) }, + where: { id: userId }, include: { emails: true, accessTokens: true, @@ -219,12 +219,12 @@ export const getAllDataForUser = async ( }); }; -export const enable2FAForUser = async (tokenUserId: string, userId: string) => { +export const enable2FAForUser = async (tokenUserId: number, userId: number) => { if (!(await can(tokenUserId, UserScopes.ENABLE_USER_2FA, "user", userId))) throw new Error(INSUFFICIENT_PERMISSION); const secret = authenticator.generateSecret(); await prisma.users.update({ - where: { id: parseInt(userId) }, + where: { id: userId }, data: { twoFactorSecret: secret }, }); await deleteItemFromCache(`cache_getUserById_${userId}`); @@ -234,8 +234,8 @@ export const enable2FAForUser = async (tokenUserId: string, userId: string) => { }; export const verify2FAForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, verificationCode: number ) => { if (!(await can(tokenUserId, UserScopes.ENABLE_USER_2FA, "user", userId))) @@ -243,7 +243,7 @@ export const verify2FAForUser = async ( const secret = ( await prisma.users.findOne({ select: { twoFactorSecret: true }, - where: { id: parseInt(userId) }, + where: { id: userId }, }) )?.twoFactorSecret; if (!secret) throw new Error(NOT_ENABLED_2FA); @@ -251,7 +251,7 @@ export const verify2FAForUser = async ( throw new Error(INVALID_2FA_TOKEN); const codes = await createBackupCodes(userId, 10); await prisma.users.update({ - where: { id: parseInt(userId) }, + where: { id: userId }, data: { twoFactorEnabled: true }, }); await deleteItemFromCache(`cache_getUserById_${userId}`); @@ -259,14 +259,14 @@ export const verify2FAForUser = async ( }; export const disable2FAForUser = async ( - tokenUserId: string, - userId: string + tokenUserId: number, + userId: number ) => { if (!(await can(tokenUserId, UserScopes.DISABLE_USER_2FA, "user", userId))) throw new Error(INSUFFICIENT_PERMISSION); - await prisma.backupCodes.deleteMany({ where: { userId: parseInt(userId) } }); + await prisma.backupCodes.deleteMany({ where: { userId: userId } }); const result = prisma.users.update({ - where: { id: parseInt(userId) }, + where: { id: userId }, data: { twoFactorEnabled: false, twoFactorSecret: null, @@ -277,8 +277,8 @@ export const disable2FAForUser = async ( }; export const regenerateBackupCodesForUser = async ( - tokenUserId: string, - userId: string + tokenUserId: number, + userId: number ) => { if ( !(await can( @@ -289,13 +289,13 @@ export const regenerateBackupCodesForUser = async ( )) ) throw new Error(INSUFFICIENT_PERMISSION); - await prisma.backupCodes.deleteMany({ where: { userId: parseInt(userId) } }); + await prisma.backupCodes.deleteMany({ where: { userId: userId } }); return createBackupCodes(userId, 10); }; export const getUserAccessTokensForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, queryParams: any ) => { if ( @@ -303,7 +303,7 @@ export const getUserAccessTokensForUser = async ( ) return paginatedResult( await prisma.accessTokens.findMany({ - where: { userId: parseInt(userId) }, + where: { userId: userId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -312,8 +312,8 @@ export const getUserAccessTokensForUser = async ( }; export const getUserAccessTokenForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, accessTokenId: string ) => { if ( @@ -326,8 +326,8 @@ export const getUserAccessTokenForUser = async ( }; export const updateAccessTokenForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, accessTokenId: string, data: accessTokensUpdateInput, locals: Locals @@ -343,8 +343,8 @@ export const updateAccessTokenForUser = async ( }; export const createAccessTokenForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, accessToken: accessTokensCreateInput, locals: Locals ) => { @@ -355,15 +355,15 @@ export const createAccessTokenForUser = async ( accessToken.expiresAt = accessToken.expiresAt || new Date(TOKEN_EXPIRY_API_KEY_MAX); return prisma.accessTokens.create({ - data: { ...accessToken, user: { connect: { id: parseInt(userId) } } }, + data: { ...accessToken, user: { connect: { id: userId } } }, }); } throw new Error(INSUFFICIENT_PERMISSION); }; export const deleteAccessTokenForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, accessTokenId: string, locals: Locals ) => { @@ -377,14 +377,14 @@ export const deleteAccessTokenForUser = async ( }; export const getUserSessionsForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, queryParams: any ) => { if (await can(tokenUserId, UserScopes.READ_USER_SESSION, "user", userId)) return paginatedResult( await prisma.sessions.findMany({ - where: { userId: parseInt(userId) }, + where: { userId: userId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -393,8 +393,8 @@ export const getUserSessionsForUser = async ( }; export const getUserSessionForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, sessionId: string ) => { if (await can(tokenUserId, UserScopes.READ_USER_SESSION, "user", userId)) @@ -403,8 +403,8 @@ export const getUserSessionForUser = async ( }; export const deleteSessionForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, sessionId: string, locals: Locals ) => { @@ -415,14 +415,14 @@ export const deleteSessionForUser = async ( }; export const getUserIdentitiesForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, queryParams: any ) => { if (await can(tokenUserId, UserScopes.READ_USER_IDENTITY, "user", userId)) return paginatedResult( await prisma.identities.findMany({ - where: { userId: parseInt(userId) }, + where: { userId: userId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -431,19 +431,19 @@ export const getUserIdentitiesForUser = async ( }; export const createUserIdentityForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, identity: identitiesCreateInput ) => { if (await can(tokenUserId, UserScopes.CREATE_USER_IDENTITY, "user", userId)) return prisma.identities.create({ - data: { ...identity, user: { connect: { id: parseInt(userId) } } }, + data: { ...identity, user: { connect: { id: userId } } }, }); throw new Error(INSUFFICIENT_PERMISSION); }; export const connectUserIdentityForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, service: string, url: string ) => { @@ -453,8 +453,8 @@ export const connectUserIdentityForUser = async ( }; export const getUserIdentityForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, identityId: string ) => { if (await can(tokenUserId, UserScopes.READ_USER_IDENTITY, "user", userId)) @@ -463,8 +463,8 @@ export const getUserIdentityForUser = async ( }; export const deleteIdentityForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, identityId: string, locals: Locals ) => { @@ -510,14 +510,14 @@ export const addInvitationCredits = async ( }; export const getAllEmailsForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, queryParams: any ) => { if (await can(tokenUserId, UserScopes.READ_USER_EMAILS, "user", userId)) { return paginatedResult( await prisma.emails.findMany({ - where: { userId: parseInt(userId) }, + where: { userId: userId }, ...queryParamsToSelect(queryParams), }), { first: queryParams.first, last: queryParams.last } @@ -527,8 +527,8 @@ export const getAllEmailsForUser = async ( }; export const getEmailForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, emailId: string ) => { if (await can(tokenUserId, UserScopes.READ_USER_EMAILS, "user", userId)) @@ -537,8 +537,8 @@ export const getEmailForUser = async ( }; export const resendEmailVerificationForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, emailId: string ) => { if ( @@ -554,8 +554,8 @@ export const resendEmailVerificationForUser = async ( }; export const addEmailToUserForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, email: string, locals: Locals ) => { @@ -565,7 +565,7 @@ export const addEmailToUserForUser = async ( const emailExistsAlready = (await prisma.emails.findMany({ where: { email } })).length !== 0; if (emailExistsAlready) throw new Error(EMAIL_EXISTS); - const result = await createEmail(parseInt(userId), email, true); + const result = await createEmail(userId, email, true); trackEvent( { userId, type: EventType.EMAIL_CREATED, data: { email } }, locals @@ -574,8 +574,8 @@ export const addEmailToUserForUser = async ( }; export const deleteEmailFromUserForUser = async ( - tokenUserId: string, - userId: string, + tokenUserId: number, + userId: number, emailId: string, locals: Locals ) => { @@ -585,8 +585,7 @@ export const deleteEmailFromUserForUser = async ( where: { id: parseInt(emailId) }, }); if (!email) throw new Error(RESOURCE_NOT_FOUND); - if (email.userId !== parseInt(userId)) - throw new Error(INSUFFICIENT_PERMISSION); + if (email.userId !== userId) throw new Error(INSUFFICIENT_PERMISSION); const verifiedEmails = await prisma.emails.findMany({ where: { id: parseInt(emailId) }, }); @@ -598,7 +597,7 @@ export const deleteEmailFromUserForUser = async ( (emailObject) => emailObject.id !== parseInt(emailId) )[0]; await prisma.users.update({ - where: { id: parseInt(userId) }, + where: { id: userId }, data: { prefersEmail: { connect: { id: nextVerifiedEmail.id } } }, }); await deleteItemFromCache(`cache_getUserById_${userId}`); @@ -614,8 +613,8 @@ export const deleteEmailFromUserForUser = async ( }; export const getMembershipDetailsForUser = async ( - userId: string, - membershipId: string + userId: number, + membershipId: number ) => { if ( await can( @@ -626,19 +625,19 @@ export const getMembershipDetailsForUser = async ( ) ) return prisma.memberships.findOne({ - where: { id: parseInt(membershipId) }, + where: { id: membershipId }, include: { user: true, group: true }, }); throw new Error(INSUFFICIENT_PERMISSION); }; export const deleteMembershipForUser = async ( - tokenUserId: string | ApiKeyResponse, - membershipId: string, + tokenUserId: number | ApiKeyResponse, + membershipId: number, locals: Locals ) => { const membership = await prisma.memberships.findOne({ - where: { id: parseInt(membershipId) }, + where: { id: membershipId }, }); if (!membership) throw new Error(MEMBERSHIP_NOT_FOUND); if ( @@ -653,11 +652,7 @@ export const deleteMembershipForUser = async ( where: { groupId: membership.groupId }, }); if (groupMembers.length === 1) - return deleteGroupForUser( - tokenUserId, - String(membership.groupId), - locals - ); + return deleteGroupForUser(tokenUserId, membership.groupId, locals); if (membership.role === "OWNER") { const currentMembers = groupMembers.filter( (member) => member.role === "OWNER" @@ -677,8 +672,8 @@ export const deleteMembershipForUser = async ( }; export const updateMembershipForUser = async ( - userId: string | ApiKeyResponse, - membershipId: string, + userId: number | ApiKeyResponse, + membershipId: number, data: membershipsUpdateInput, locals: Locals ) => { @@ -691,7 +686,7 @@ export const updateMembershipForUser = async ( ) ) { const membership = await prisma.memberships.findOne({ - where: { id: parseInt(membershipId) }, + where: { id: membershipId }, }); if (!membership) throw new Error(MEMBERSHIP_NOT_FOUND); if (data.role !== membership.role) { @@ -714,7 +709,7 @@ export const updateMembershipForUser = async ( locals ); return prisma.memberships.update({ - where: { id: parseInt(membershipId) }, + where: { id: membershipId }, data, }); } diff --git a/src/_staart/services/group.service.ts b/src/_staart/services/group.service.ts index 06497d4c2..8092a2230 100644 --- a/src/_staart/services/group.service.ts +++ b/src/_staart/services/group.service.ts @@ -36,7 +36,7 @@ import { KeyValue } from "../interfaces/general"; */ export const createGroup = async ( group: groupsCreateInput, - ownerId: string + ownerId: number ) => { if (!group.name) throw new Error(INVALID_INPUT); group.name = capitalizeFirstAndLastLetter(group.name); @@ -53,7 +53,7 @@ export const createGroup = async ( await prisma.memberships.create({ data: { role: "OWNER", - user: { connect: { id: parseInt(ownerId) } }, + user: { connect: { id: ownerId } }, group: { connect: { id: result.id } }, }, });