first commit
This commit is contained in:
1
calcom/packages/platform/utils/README.md
Normal file
1
calcom/packages/platform/utils/README.md
Normal file
@@ -0,0 +1 @@
|
||||
Contains utils shared by platform api and packages
|
||||
1
calcom/packages/platform/utils/index.ts
Normal file
1
calcom/packages/platform/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./permissions";
|
||||
9
calcom/packages/platform/utils/jest.config.ts
Normal file
9
calcom/packages/platform/utils/jest.config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
// jest.config.ts
|
||||
import type { Config } from "@jest/types";
|
||||
|
||||
const config: Config.InitialOptions = {
|
||||
preset: "ts-jest",
|
||||
testEnvironment: "node",
|
||||
verbose: true,
|
||||
};
|
||||
export default config;
|
||||
19
calcom/packages/platform/utils/package.json
Normal file
19
calcom/packages/platform/utils/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@calcom/platform-utils",
|
||||
"version": "0.0.0",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "jest ./tests",
|
||||
"build": "tsc --build --force tsconfig.json",
|
||||
"build:watch": "tsc --build --force ./tsconfig.json --watch",
|
||||
"post-install": "yarn build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@calcom/platform-constants": "*",
|
||||
"@calcom/platform-types": "*",
|
||||
"@types/jest": "^29.5.10",
|
||||
"jest": "^29.7.0",
|
||||
"ts-jest": "^29.1.1"
|
||||
}
|
||||
}
|
||||
73
calcom/packages/platform/utils/permissions.ts
Normal file
73
calcom/packages/platform/utils/permissions.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import {
|
||||
BOOKING_READ,
|
||||
BOOKING_WRITE,
|
||||
EVENT_TYPE_READ,
|
||||
EVENT_TYPE_WRITE,
|
||||
PERMISSIONS,
|
||||
SCHEDULE_READ,
|
||||
SCHEDULE_WRITE,
|
||||
APPS_READ,
|
||||
APPS_WRITE,
|
||||
PROFILE_READ,
|
||||
PROFILE_WRITE,
|
||||
} from "@calcom/platform-constants";
|
||||
import type { PLATFORM_PERMISSION } from "@calcom/platform-types";
|
||||
|
||||
export const hasPermission = (userPermissions: number, permission: PLATFORM_PERMISSION): boolean => {
|
||||
// use bitwise AND to check if user has the permission
|
||||
return (userPermissions & permission) === permission;
|
||||
};
|
||||
|
||||
export const hasPermissions = (userPermissions: number, permissions: PLATFORM_PERMISSION[]): boolean => {
|
||||
// use bitwise AND to check if each required permission is present
|
||||
return permissions.every((permission) => hasPermission(userPermissions, permission));
|
||||
};
|
||||
|
||||
export const hasEventTypeReadPermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, EVENT_TYPE_READ);
|
||||
};
|
||||
|
||||
export const hasEventTypeWritePermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, EVENT_TYPE_WRITE);
|
||||
};
|
||||
|
||||
export const hasBookingReadPermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, BOOKING_READ);
|
||||
};
|
||||
|
||||
export const hasBookingWritePermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, BOOKING_WRITE);
|
||||
};
|
||||
|
||||
export const hasScheduleReadPermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, SCHEDULE_READ);
|
||||
};
|
||||
|
||||
export const hasScheduleWritePermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, SCHEDULE_WRITE);
|
||||
};
|
||||
|
||||
export const hasAppsReadPermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, APPS_READ);
|
||||
};
|
||||
|
||||
export const hasAppsWritePermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, APPS_WRITE);
|
||||
};
|
||||
|
||||
export const hasProfileReadPermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, PROFILE_READ);
|
||||
};
|
||||
|
||||
export const hasProfileWritePermission = (userPermissions: number): boolean => {
|
||||
return hasPermission(userPermissions, PROFILE_WRITE);
|
||||
};
|
||||
|
||||
export const listPermissions = (userPermissions: number): PLATFORM_PERMISSION[] => {
|
||||
return PERMISSIONS.reduce((acc, permission) => {
|
||||
if (hasPermission(userPermissions, permission)) {
|
||||
return [...acc, permission];
|
||||
}
|
||||
return acc;
|
||||
}, [] as PLATFORM_PERMISSION[]);
|
||||
};
|
||||
91
calcom/packages/platform/utils/tests/permissions.test.ts
Normal file
91
calcom/packages/platform/utils/tests/permissions.test.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
BOOKING_READ,
|
||||
BOOKING_WRITE,
|
||||
EVENT_TYPE_READ,
|
||||
EVENT_TYPE_WRITE,
|
||||
SCHEDULE_READ,
|
||||
SCHEDULE_WRITE,
|
||||
} from "@calcom/platform-constants";
|
||||
|
||||
import { hasPermission, hasPermissions, listPermissions } from "../permissions";
|
||||
|
||||
describe("Permissions Function: hasPermission", () => {
|
||||
let userPermissions: number;
|
||||
beforeEach(() => {
|
||||
userPermissions = 0;
|
||||
});
|
||||
it("it should return true if user has the permission", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
const result = hasPermission(userPermissions, EVENT_TYPE_READ);
|
||||
expect(result).toEqual(true);
|
||||
});
|
||||
|
||||
it("it should return false if user does not have the permission", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
const result = hasPermission(userPermissions, BOOKING_WRITE);
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
|
||||
it("it should return false if user does not have the permission", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
const result = hasPermission(userPermissions, SCHEDULE_WRITE);
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Permissions Function: hasPermissions", () => {
|
||||
let userPermissions: number;
|
||||
beforeEach(() => {
|
||||
userPermissions = 0;
|
||||
});
|
||||
it("it should return true if user has all the permissions", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
const result = hasPermissions(userPermissions, [EVENT_TYPE_READ, SCHEDULE_READ]);
|
||||
expect(result).toEqual(true);
|
||||
});
|
||||
|
||||
it("it should return false if user does not have all the permissions", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
const result = hasPermissions(userPermissions, [BOOKING_WRITE, SCHEDULE_READ, EVENT_TYPE_READ]);
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
|
||||
it("it should return false if user does not have all permissions", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
const result = hasPermissions(userPermissions, [SCHEDULE_WRITE, SCHEDULE_READ]);
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Permissions Function: listPermission", () => {
|
||||
let userPermissions: number;
|
||||
beforeEach(() => {
|
||||
userPermissions = 0;
|
||||
});
|
||||
it("it should return the list of permissions a user has", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
const result = listPermissions(userPermissions);
|
||||
const shouldContainPermissions = [EVENT_TYPE_READ, SCHEDULE_READ];
|
||||
const shouldNotContainPermissions = [SCHEDULE_WRITE, EVENT_TYPE_WRITE, BOOKING_READ, BOOKING_WRITE];
|
||||
shouldContainPermissions.forEach((permission) => expect(result).toContain(permission));
|
||||
shouldNotContainPermissions.forEach((permission) => expect(result).not.toContain(permission));
|
||||
});
|
||||
it("it should return the list of permissions a user has", () => {
|
||||
userPermissions |= EVENT_TYPE_READ;
|
||||
userPermissions |= SCHEDULE_READ;
|
||||
userPermissions |= BOOKING_WRITE;
|
||||
userPermissions |= BOOKING_READ;
|
||||
const result = listPermissions(userPermissions);
|
||||
const shouldContainPermissions = [EVENT_TYPE_READ, SCHEDULE_READ, BOOKING_WRITE, BOOKING_READ];
|
||||
const shouldNotContainPermissions = [SCHEDULE_WRITE, EVENT_TYPE_WRITE];
|
||||
shouldContainPermissions.forEach((permission) => expect(result).toContain(permission));
|
||||
shouldNotContainPermissions.forEach((permission) => expect(result).not.toContain(permission));
|
||||
});
|
||||
});
|
||||
13
calcom/packages/platform/utils/tsconfig.json
Normal file
13
calcom/packages/platform/utils/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "@calcom/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"target": "ES5",
|
||||
"resolveJsonModule": true,
|
||||
"types": ["jest"],
|
||||
"outDir": "./dist",
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true
|
||||
},
|
||||
"include": [".", "./tests", "../../types/business-days-plugin.d.ts", "../../types/window.d.ts"],
|
||||
"exclude": ["dist", "build", "node_modules", "**/*.test.*", "**/__mocks__/*", "**/__tests__/*"]
|
||||
}
|
||||
Reference in New Issue
Block a user