From 95b7b14ff6e3c106171cd0aaf87ba942c04ce721 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 5 Dec 2024 16:47:06 +0000 Subject: [PATCH] User API typing. --- packages/backend-core/src/cache/invite.ts | 10 +--- packages/types/src/api/web/user.ts | 55 ++++++++++++++++++- .../src/api/controllers/global/users.ts | 55 ++++++++++++++----- 3 files changed, 96 insertions(+), 24 deletions(-) diff --git a/packages/backend-core/src/cache/invite.ts b/packages/backend-core/src/cache/invite.ts index e3d698bcc6..5d896ba04c 100644 --- a/packages/backend-core/src/cache/invite.ts +++ b/packages/backend-core/src/cache/invite.ts @@ -3,18 +3,10 @@ import { Duration } from "../utils" import env from "../environment" import { getTenantId } from "../context" import * as redis from "../redis/init" +import { Invite, InviteWithCode } from "@budibase/types" const TTL_SECONDS = Duration.fromDays(7).toSeconds() -interface Invite { - email: string - info: any -} - -interface InviteWithCode extends Invite { - code: string -} - /** * Given an invite code and invite body, allow the update an existing/valid invite in redis * @param code The invite code for an invite in redis diff --git a/packages/types/src/api/web/user.ts b/packages/types/src/api/web/user.ts index e102f136c3..2f83f012b3 100644 --- a/packages/types/src/api/web/user.ts +++ b/packages/types/src/api/web/user.ts @@ -1,6 +1,15 @@ -import { User } from "../../documents" +import { AccountMetadata, PlatformUser, User } from "../../documents" import { SearchFilters } from "../../sdk" +export interface Invite { + email: string + info: any +} + +export interface InviteWithCode extends Invite { + code: string +} + export interface SaveUserResponse { _id: string _rev: string @@ -47,6 +56,11 @@ export interface InviteUserRequest { email: string userInfo: any } +export interface InviteUserResponse { + message: string + successful: { email: string }[] + unsuccessful: { email: string; reason: string }[] +} export interface DeleteInviteUserRequest { code: string @@ -54,6 +68,9 @@ export interface DeleteInviteUserRequest { export type InviteUsersRequest = InviteUserRequest[] export type DeleteInviteUsersRequest = DeleteInviteUserRequest[] +export interface DeleteInviteUsersResponse { + message: string +} export interface InviteUsersResponse { successful: { email: string }[] @@ -68,6 +85,17 @@ export interface SearchUsersRequest { limit?: number paginate?: boolean } +export interface SearchUsersResponse { + data: User[] + hasNextPage?: boolean + nextPage?: string +} + +export type FetchUsersResponse = User[] + +export interface FindUserResponse extends User {} + +export type LookupTenantUserResponse = PlatformUser export interface CreateAdminUserRequest { email: string @@ -106,3 +134,28 @@ export interface AcceptUserInviteResponse { export interface SyncUserRequest { previousUser?: User } + +export interface DeleteUserResponse { + message: string +} + +export interface CountUserResponse { + userCount: number +} + +export interface CheckInviteResponse { + email: string +} + +export type GetUserInvitesResponse = InviteWithCode[] + +export interface UpdateInviteRequest extends Omit { + email?: string + builder?: { + apps: string[] + } + apps: string[] +} +export interface UpdateInviteResponse extends Invite {} + +export type LookupAccountHolderResponse = AccountMetadata | null diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index e977d5ff5d..1d36dd524c 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -6,21 +6,34 @@ import { AddSSoUserRequest, BulkUserRequest, BulkUserResponse, + CheckInviteResponse, + CountUserResponse, CreateAdminUserRequest, CreateAdminUserResponse, Ctx, DeleteInviteUserRequest, DeleteInviteUsersRequest, + DeleteInviteUsersResponse, + DeleteUserResponse, + FetchUsersResponse, + FindUserResponse, + GetUserInvitesResponse, Hosting, InviteUserRequest, + InviteUserResponse, InviteUsersRequest, InviteUsersResponse, LockName, LockType, + LookupAccountHolderResponse, + LookupTenantUserResponse, MigrationType, PlatformUserByEmail, SaveUserResponse, SearchUsersRequest, + SearchUsersResponse, + UpdateInviteRequest, + UpdateInviteResponse, User, UserCtx, UserIdentifier, @@ -80,7 +93,7 @@ export const save = async (ctx: UserCtx) => { } } -export const addSsoSupport = async (ctx: Ctx) => { +export const addSsoSupport = async (ctx: Ctx) => { const { email, ssoId } = ctx.request.body try { // Status is changed to 404 from getUserDoc if user is not found @@ -207,7 +220,7 @@ export const adminUser = async ( }) } -export const countByApp = async (ctx: any) => { +export const countByApp = async (ctx: UserCtx) => { const appId = ctx.params.appId try { ctx.body = await userSdk.db.countUsersByApp(appId) @@ -216,7 +229,7 @@ export const countByApp = async (ctx: any) => { } } -export const destroy = async (ctx: any) => { +export const destroy = async (ctx: UserCtx) => { const id = ctx.params.id if (id === ctx.user._id) { ctx.throw(400, "Unable to delete self.") @@ -239,7 +252,9 @@ export const getAppUsers = async (ctx: Ctx) => { ctx.body = { data: users } } -export const search = async (ctx: Ctx) => { +export const search = async ( + ctx: Ctx +) => { const body = ctx.request.body // TODO: for now only two supported search keys; string.email and equal._id @@ -280,7 +295,7 @@ export const search = async (ctx: Ctx) => { } // called internally by app server user fetch -export const fetch = async (ctx: any) => { +export const fetch = async (ctx: UserCtx) => { const all = await userSdk.db.allUsers() // user hashed password shouldn't ever be returned for (let user of all) { @@ -292,11 +307,13 @@ export const fetch = async (ctx: any) => { } // called internally by app server user find -export const find = async (ctx: any) => { +export const find = async (ctx: UserCtx) => { ctx.body = await userSdk.db.getUser(ctx.params.id) } -export const tenantUserLookup = async (ctx: any) => { +export const tenantUserLookup = async ( + ctx: UserCtx +) => { const id = ctx.params.id // is email, check its valid if (id.includes("@") && !emailValidator.validate(id)) { @@ -314,7 +331,9 @@ export const tenantUserLookup = async (ctx: any) => { * This will be paginated to a default of the first 50 users, * So the account holder may not be found until further pagination has occurred */ -export const accountHolderLookup = async (ctx: Ctx) => { +export const accountHolderLookup = async ( + ctx: Ctx +) => { try { const users = await userSdk.core.getAllUsers() const response = await userSdk.core.getExistingAccounts( @@ -366,7 +385,9 @@ export const onboardUsers = async ( ctx.body = { ...resp, created: true } } -export const invite = async (ctx: Ctx) => { +export const invite = async ( + ctx: Ctx +) => { const request = ctx.request.body let multiRequest = [request] @@ -389,12 +410,14 @@ export const invite = async (ctx: Ctx) => { } } -export const inviteMultiple = async (ctx: Ctx) => { +export const inviteMultiple = async ( + ctx: Ctx +) => { ctx.body = await userSdk.invite(ctx.request.body) } export const removeMultipleInvites = async ( - ctx: Ctx + ctx: Ctx ) => { const inviteCodesToRemove = ctx.request.body.map( (invite: DeleteInviteUserRequest) => invite.code @@ -407,7 +430,7 @@ export const removeMultipleInvites = async ( } } -export const checkInvite = async (ctx: any) => { +export const checkInvite = async (ctx: UserCtx) => { const { code } = ctx.params let invite try { @@ -422,7 +445,9 @@ export const checkInvite = async (ctx: any) => { } } -export const getUserInvites = async (ctx: any) => { +export const getUserInvites = async ( + ctx: UserCtx +) => { try { // Restricted to the currently authenticated tenant ctx.body = await cache.invite.getInviteCodes() @@ -431,7 +456,9 @@ export const getUserInvites = async (ctx: any) => { } } -export const updateInvite = async (ctx: any) => { +export const updateInvite = async ( + ctx: UserCtx +) => { const { code } = ctx.params let updateBody = { ...ctx.request.body }