Skip to content

Commit

Permalink
Refactor sdk-utils to export additional functions and types
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Dec 10, 2024
1 parent c55daf1 commit 466adbd
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 1 deletion.
77 changes: 77 additions & 0 deletions packages/studiocms_core/src/sdk-utils/get/getPermissionLists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { db } from 'astro:db';
import { tsPermissions, tsUsers } from '../../db/tsTables';
import type { AvailableLists, PermissionsList } from '../types';
import { combineRanks, verifyRank } from '../utils';

/**
* Retrieves a list of permissions based on the specified list type.
*
* @param list - The type of list to retrieve. Can be one of 'all', 'owners', 'admins', 'editors', or 'visitors'.
* @returns A promise that resolves to an array of permissions lists.
*
* The function performs the following actions based on the list type:
* - 'all': Retrieves all users and their permissions, then categorizes them into owners, admins, editors, and visitors.
* - 'owners': Retrieves users with 'owner' permissions.
* - 'admins': Retrieves users with 'admin' permissions.
* - 'editors': Retrieves users with 'editor' permissions.
* - 'visitors': Retrieves users with 'visitor' permissions.
*
* The function uses the following helper functions:
* - `verifyRank`: Verifies the rank of users based on the existing users and current permitted users.
* - `combineRanks`: Combines users of a specific rank into a single list.
*
* @example
* ```typescript
* const owners = await getPermissionsLists('owners');
* console.log(owners);
* ```
*/
export async function getPermissionsLists(list: AvailableLists): Promise<PermissionsList[]> {
switch (list) {
case 'all': {
const currentPermittedUsers = await db.select().from(tsPermissions);
const existingUsers = await db.select().from(tsUsers);

const owners = verifyRank(existingUsers, currentPermittedUsers, 'owner');

const admins = verifyRank(existingUsers, currentPermittedUsers, 'admin');

const editors = verifyRank(existingUsers, currentPermittedUsers, 'editor');

const visitors = verifyRank(existingUsers, currentPermittedUsers, 'visitor');

return [
...combineRanks('owner', owners),
...combineRanks('admin', admins),
...combineRanks('editor', editors),
...combineRanks('visitor', visitors),
];
}
case 'owners': {
const currentPermittedUsers = await db.select().from(tsPermissions);
const existingUsers = await db.select().from(tsUsers);

return verifyRank(existingUsers, currentPermittedUsers, 'owner');
}
case 'admins': {
const currentPermittedUsers = await db.select().from(tsPermissions);
const existingUsers = await db.select().from(tsUsers);

return verifyRank(existingUsers, currentPermittedUsers, 'admin');
}
case 'editors': {
const currentPermittedUsers = await db.select().from(tsPermissions);
const existingUsers = await db.select().from(tsUsers);

return verifyRank(existingUsers, currentPermittedUsers, 'editor');
}
case 'visitors': {
const currentPermittedUsers = await db.select().from(tsPermissions);
const existingUsers = await db.select().from(tsUsers);

return verifyRank(existingUsers, currentPermittedUsers, 'visitor');
}
}
}

export default getPermissionsLists;
1 change: 1 addition & 0 deletions packages/studiocms_core/src/sdk-utils/get/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as getDatabase } from './getDatabase';
export { default as getDatabaseEntry } from './getDatabaseEntry';
export { default as getDatabaseTable } from './getDatabaseTable';
export { default as getPackagePages } from './getPackagePages';
export { default as getPermissionsLists } from './getPermissionLists';
9 changes: 8 additions & 1 deletion packages/studiocms_core/src/sdk-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getDatabase, getDatabaseEntry, getDatabaseTable, getPackagePages } from './get';
import {
getDatabase,
getDatabaseEntry,
getDatabaseTable,
getPackagePages,
getPermissionsLists,
} from './get';
import type { STUDIOCMS_SDK } from './types';

/**
Expand All @@ -24,6 +30,7 @@ export const StudioCMS_SDK: STUDIOCMS_SDK = {
databaseEntry: (database) => getDatabaseEntry(database),
databaseTable: async (database) => await getDatabaseTable(database),
packagePages: async (packageName) => await getPackagePages(packageName),
permissionsLists: async (list) => await getPermissionsLists(list),
},
};

Expand Down
62 changes: 62 additions & 0 deletions packages/studiocms_core/src/sdk-utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,43 @@ export type DatabaseTables =
*/
export type GetDatabase = SiteConfig | CombinedUserData[] | CombinedPageData[] | undefined;

/**
* Represents a combined rank with associated details.
*
* @property {string} rank - The rank of the entity.
* @property {string} id - The unique identifier for the rank.
* @property {string} name - The name associated with the rank.
*/
export type CombinedRank = {
rank: string;
id: string;
name: string;
};

/**
* Represents a single rank with an identifier and a name.
*/
export type SingleRank = {
id: string;
name: string;
};

/**
* Represents a list of permissions which can be either a combined rank or a single rank.
*/
export type PermissionsList = CombinedRank | SingleRank;

/**
* Represents the different types of user lists available in the system.
*
* - 'owners': List of owners.
* - 'admins': List of administrators.
* - 'editors': List of editors.
* - 'visitors': List of visitors.
* - 'all': List of all users.
*/
export type AvailableLists = 'owners' | 'admins' | 'editors' | 'visitors' | 'all';

/**
* Interface representing the STUDIOCMS SDK.
*/
Expand Down Expand Up @@ -363,5 +400,30 @@ export interface STUDIOCMS_SDK {
* @returns A promise that resolves to an array of CombinedPageData objects.
*/
packagePages: (packageName: string) => Promise<CombinedPageData[]>;

/**
* Retrieves a list of permissions based on the specified list type.
*
* @param list - The type of list to retrieve. Can be one of 'all', 'owners', 'admins', 'editors', or 'visitors'.
* @returns A promise that resolves to an array of permissions lists.
*
* The function performs the following actions based on the list type:
* - 'all': Retrieves all users and their permissions, then categorizes them into owners, admins, editors, and visitors.
* - 'owners': Retrieves users with 'owner' permissions.
* - 'admins': Retrieves users with 'admin' permissions.
* - 'editors': Retrieves users with 'editor' permissions.
* - 'visitors': Retrieves users with 'visitor' permissions.
*
* The function uses the following helper functions:
* - `verifyRank`: Verifies the rank of users based on the existing users and current permitted users.
* - `combineRanks`: Combines users of a specific rank into a single list.
*
* @example
* ```typescript
* const owners = await getPermissionsLists('owners');
* console.log(owners);
* ```
*/
permissionsLists: (list: AvailableLists) => Promise<PermissionsList[]>;
};
}
41 changes: 41 additions & 0 deletions packages/studiocms_core/src/sdk-utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
} from '../db/tsTables';
import type {
CombinedPageData,
CombinedRank,
CombinedUserData,
SingleRank,
tsPageDataCategoriesSelect,
tsPageDataSelect,
tsPageDataTagsSelect,
tsPermissionsSelect,
tsUsersSelect,
} from './types';

Expand Down Expand Up @@ -99,3 +102,41 @@ export async function collectPageData(page: tsPageDataSelect): Promise<CombinedP
defaultContent: defaultContentData,
};
}

/**
* Verifies and filters users based on their rank and permissions.
*
* @param users - An array of user objects to be verified.
* @param permissions - An array of permission objects to check against.
* @param rank - The rank to filter users by.
* @returns An array of objects containing the id and name of users with the specified rank.
*/
export function verifyRank(
users: tsUsersSelect[],
permissions: tsPermissionsSelect[],
rank: string
): SingleRank[] {
const filteredUsers = permissions.filter((user) => user.rank === rank);
const permitted: { id: string; name: string }[] = [];

for (const user of filteredUsers) {
const foundUser = users.find((u) => u.id === user.user);

if (foundUser) {
permitted.push({ id: foundUser.id, name: foundUser.name });
}
}

return permitted;
}

/**
* Combines a given rank with an array of users, returning a new array where each user is combined with the rank.
*
* @param rank - The rank to be combined with each user.
* @param users - An array of users, each represented by a SingleRank object.
* @returns An array of CombinedRank objects, where each object contains the given rank and the properties of a user.
*/
export function combineRanks(rank: string, users: SingleRank[]): CombinedRank[] {
return users.map((user) => ({ rank, ...user }));
}

0 comments on commit 466adbd

Please sign in to comment.