Skip to content

Commit

Permalink
Merge pull request #26861 from software-mansion-labs/ts-migration/per…
Browse files Browse the repository at this point in the history
…missions-lib

[No QA] [TS migration] Migrate 'Permissions.js' lib to TypeScript
  • Loading branch information
Gonals authored Sep 21, 2023
2 parents f65c58e + 934a366 commit 832a12b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 158 deletions.
34 changes: 16 additions & 18 deletions src/libs/Log.js → src/libs/Log.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
// Making an exception to this rule here since we don't need an "action" for Log and Log should just be used directly. Creating a Log
// action would likely cause confusion about which one to use. But most other API methods should happen inside an action file.
/* eslint-disable rulesdir/no-api-in-views */
import {Merge} from 'type-fest';
import Logger from 'expensify-common/lib/Logger';
import getPlatform from './getPlatform';
import pkg from '../../package.json';
import requireParameters from './requireParameters';
import * as Network from './Network';

let timeout = null;
let timeout: NodeJS.Timeout;

/**
* @param {Object} parameters
* @param {String} parameters.expensifyCashAppVersion
* @param {Object[]} parameters.logPacket
* @returns {Promise}
*/
function LogCommand(parameters) {
type LogCommandParameters = {
expensifyCashAppVersion: string;
logPacket: string;
};

function LogCommand(parameters: LogCommandParameters): Promise<{requestID: string}> {
const commandName = 'Log';
requireParameters(['logPacket', 'expensifyCashAppVersion'], parameters, commandName);

// Note: We are forcing Log to run since it requires no authToken and should only be queued when we are offline.
// Non-cancellable request: during logout, when requests are cancelled, we don't want to cancel any remaining logs
return Network.post(commandName, {...parameters, forceNetworkRequest: true, canCancel: false});
return Network.post(commandName, {...parameters, forceNetworkRequest: true, canCancel: false}) as Promise<{requestID: string}>;
}

// eslint-disable-next-line
type ServerLoggingCallbackOptions = {api_setCookie: boolean; logPacket: string};
type RequestParams = Merge<ServerLoggingCallbackOptions, {shouldProcessImmediately: boolean; shouldRetry: boolean; expensifyCashAppVersion: string; parameters: string}>;

/**
* Network interface for logger.
*
* @param {Logger} logger
* @param {Object} params
* @param {Object} params.parameters
* @param {String} params.message
* @return {Promise}
*/
function serverLoggingCallback(logger, params) {
const requestParams = params;
function serverLoggingCallback(logger: Logger, params: ServerLoggingCallbackOptions): Promise<{requestID: string}> {
const requestParams = params as RequestParams;
requestParams.shouldProcessImmediately = false;
requestParams.shouldRetry = false;
requestParams.expensifyCashAppVersion = `expensifyCash[${getPlatform()}]${pkg.version}`;
if (requestParams.parameters) {
requestParams.parameters = JSON.stringify(params.parameters);
requestParams.parameters = JSON.stringify(requestParams.parameters);
}
clearTimeout(timeout);
timeout = setTimeout(() => logger.info('Flushing logs older than 10 minutes', true, {}, true), 10 * 60 * 1000);
Expand Down
126 changes: 0 additions & 126 deletions src/libs/Permissions.js

This file was deleted.

80 changes: 80 additions & 0 deletions src/libs/Permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import CONST from '../CONST';
import Beta from '../types/onyx/Beta';

function canUseAllBetas(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.ALL);
}

function canUseChronos(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.CHRONOS_IN_CASH) || canUseAllBetas(betas);
}

function canUsePayWithExpensify(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.PAY_WITH_EXPENSIFY) || canUseAllBetas(betas);
}

function canUseDefaultRooms(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.DEFAULT_ROOMS) || canUseAllBetas(betas);
}

/**
* IOU Send feature is temporarily disabled.
*/
function canUseIOUSend(): boolean {
return false;
}

function canUseWallet(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.BETA_EXPENSIFY_WALLET) || canUseAllBetas(betas);
}

function canUseCommentLinking(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.BETA_COMMENT_LINKING) || canUseAllBetas(betas);
}

/**
* We're requiring you to be added to the policy rooms beta on dev,
* since contributors have been reporting a number of false issues related to the feature being under development.
* See https://expensify.slack.com/archives/C01GTK53T8Q/p1641921996319400?thread_ts=1641598356.166900&cid=C01GTK53T8Q
*/
function canUsePolicyRooms(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.POLICY_ROOMS) || canUseAllBetas(betas);
}

function canUseTasks(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.TASKS) || canUseAllBetas(betas);
}

function canUseCustomStatus(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.CUSTOM_STATUS) || canUseAllBetas(betas);
}

function canUseCategories(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.NEW_DOT_CATEGORIES) || canUseAllBetas(betas);
}

function canUseTags(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.NEW_DOT_TAGS) || canUseAllBetas(betas);
}

/**
* Link previews are temporarily disabled.
*/
function canUseLinkPreviews(): boolean {
return false;
}

export default {
canUseChronos,
canUsePayWithExpensify,
canUseDefaultRooms,
canUseIOUSend,
canUseWallet,
canUseCommentLinking,
canUsePolicyRooms,
canUseTasks,
canUseCustomStatus,
canUseCategories,
canUseTags,
canUseLinkPreviews,
};
9 changes: 0 additions & 9 deletions src/libs/__mocks__/Log.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/libs/__mocks__/Log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Set up manual mocks for any Logging methods that are supposed hit the 'server',
// this is needed because before, the Logging queue would get flushed while tests were running,
// causing unexpected calls to HttpUtils.xhr() which would cause mock mismatches and flaky tests.
export default {
info: (message: string) => console.debug(`[info] ${message} (mocked)`),
alert: (message: string) => console.debug(`[alert] ${message} (mocked)`),
warn: (message: string) => console.debug(`[warn] ${message} (mocked)`),
hmmm: (message: string) => console.debug(`[hmmm] ${message} (mocked)`),
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'underscore';
import CONST from '../../CONST';
import Beta from '../../types/onyx/Beta';

/**
* This module is mocked in tests because all the permission methods call canUseAllBetas() and that will
Expand All @@ -10,8 +10,8 @@ import CONST from '../../CONST';

export default {
...jest.requireActual('../Permissions'),
canUseDefaultRooms: (betas) => _.contains(betas, CONST.BETAS.DEFAULT_ROOMS),
canUsePolicyRooms: (betas) => _.contains(betas, CONST.BETAS.POLICY_ROOMS),
canUseIOUSend: (betas) => _.contains(betas, CONST.BETAS.IOU_SEND),
canUseCustomStatus: (betas) => _.contains(betas, CONST.BETAS.CUSTOM_STATUS),
canUseDefaultRooms: (betas: Beta[]) => betas.includes(CONST.BETAS.DEFAULT_ROOMS),
canUsePolicyRooms: (betas: Beta[]) => betas.includes(CONST.BETAS.POLICY_ROOMS),
canUseIOUSend: (betas: Beta[]) => betas.includes(CONST.BETAS.IOU_SEND),
canUseCustomStatus: (betas: Beta[]) => betas.includes(CONST.BETAS.CUSTOM_STATUS),
};

0 comments on commit 832a12b

Please sign in to comment.