Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

let allPolicies: OnyxCollection<Policy>;

Onyx.connect({

Check warning on line 63 in src/libs/PolicyUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
Expand Down Expand Up @@ -536,6 +536,27 @@
.sort((tagA, tagB) => tagA.orderWeight - tagB.orderWeight);
}

/**
* Checks if a policy has any tags
*/
function hasTags(policyTagList: OnyxEntry<PolicyTagLists>): boolean {
const tagLists = getTagLists(policyTagList);
return tagLists.some((tagList) => Object.keys(tagList.tags ?? {}).length > 0);
}

/**
* Checks if a policy has any custom categories (categories not in the default list)
*/
function hasCustomCategories(policyCategories: OnyxEntry<PolicyCategories>): boolean {
if (!policyCategories) {
return false;
}

const defaultCategoryNames = new Set<string>(Object.values(CONST.POLICY.DEFAULT_CATEGORIES));

return Object.values(policyCategories).some((category) => category && category.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !defaultCategoryNames.has(category.name));
}

/**
* Gets a tag list of a policy by a tag index
*/
Expand Down Expand Up @@ -1709,6 +1730,8 @@
getTagListByOrderWeight,
getTagListName,
getTagLists,
hasTags,
hasCustomCategories,
getTaxByID,
getUnitRateValue,
getRateDisplayValue,
Expand Down
12 changes: 12 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11613,6 +11613,8 @@ function prepareOnboardingOnyxData({

let createWorkspaceTaskReportID;
let addExpenseApprovalsTaskReportID;
let setupTagsTaskReportID;
let setupCategoriesAndTagsTaskReportID;
const tasksData = onboardingMessage.tasks
.filter((task) => {
if (engagementChoice === CONST.ONBOARDING_CHOICES.MANAGE_TEAM) {
Expand Down Expand Up @@ -11694,6 +11696,12 @@ function prepareOnboardingOnyxData({
if (task.type === CONST.ONBOARDING_TASK_TYPE.ADD_EXPENSE_APPROVALS) {
addExpenseApprovalsTaskReportID = currentTask.reportID;
}
if (task.type === CONST.ONBOARDING_TASK_TYPE.SETUP_TAGS) {
setupTagsTaskReportID = currentTask.reportID;
}
if (task.type === CONST.ONBOARDING_TASK_TYPE.SETUP_CATEGORIES_AND_TAGS) {
setupCategoriesAndTagsTaskReportID = currentTask.reportID;
}

return {
task,
Expand Down Expand Up @@ -11901,6 +11909,8 @@ function prepareOnboardingOnyxData({
choice: engagementChoice,
createWorkspace: createWorkspaceTaskReportID,
addExpenseApprovals: addExpenseApprovalsTaskReportID,
setupTags: setupTagsTaskReportID,
setupCategoriesAndTags: setupCategoriesAndTagsTaskReportID,
},
},
);
Expand Down Expand Up @@ -11970,6 +11980,8 @@ function prepareOnboardingOnyxData({
choice: null,
createWorkspace: null,
addExpenseApprovals: null,
setupCategoriesAndTags: null,
Comment thread
mukhrr marked this conversation as resolved.
Comment thread
mukhrr marked this conversation as resolved.
setupTags: null,
},
},
);
Expand Down
110 changes: 90 additions & 20 deletions src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ import type {ApprovalRule, ExpenseRule, MccGroup} from '@src/types/onyx/Policy';
import type {PolicyCategoryExpenseLimitType} from '@src/types/onyx/PolicyCategory';
import type {OnyxData} from '@src/types/onyx/Request';

type CreatePolicyCategoryParams = {
policyID: string;
categoryName: string;
isSetupCategoriesTaskParentReportArchived: boolean;
setupCategoryTaskReport: OnyxEntry<Report>;
setupCategoryTaskParentReport: OnyxEntry<Report>;
currentUserAccountID: number;
hasOutstandingChildTask: boolean;
parentReportAction: OnyxEntry<ReportAction>;
setupCategoriesAndTagsTaskReport?: OnyxEntry<Report>;
setupCategoriesAndTagsTaskParentReport?: OnyxEntry<Report>;
isSetupCategoriesAndTagsTaskParentReportArchived?: boolean;
setupCategoriesAndTagsHasOutstandingChildTask?: boolean;
setupCategoriesAndTagsParentReportAction?: OnyxEntry<ReportAction>;
policyHasTags?: boolean;
};

type SetWorkspaceCategoryEnabledParams = {
policyData: PolicyData;
categoriesToUpdate: Record<string, {name: string; enabled: boolean}>;
isSetupCategoriesTaskParentReportArchived: boolean;
setupCategoryTaskReport: OnyxEntry<Report>;
setupCategoryTaskParentReport: OnyxEntry<Report>;
currentUserAccountID: number;
hasOutstandingChildTask: boolean;
parentReportAction: OnyxEntry<ReportAction> | undefined;
setupCategoriesAndTagsTaskReport?: OnyxEntry<Report>;
setupCategoriesAndTagsTaskParentReport?: OnyxEntry<Report>;
isSetupCategoriesAndTagsTaskParentReportArchived?: boolean;
setupCategoriesAndTagsHasOutstandingChildTask?: boolean;
setupCategoriesAndTagsParentReportAction?: OnyxEntry<ReportAction>;
policyHasTags?: boolean;
};

function appendSetupCategoriesOnboardingData(
onyxData: OnyxData<
typeof ONYXKEYS.COLLECTION.POLICY_CATEGORIES | typeof ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT | typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS
Expand Down Expand Up @@ -328,16 +362,22 @@ function getPolicyCategories(policyID: string) {
API.read(READ_COMMANDS.GET_POLICY_CATEGORIES, params);
}

function setWorkspaceCategoryEnabled(
policyData: PolicyData,
categoriesToUpdate: Record<string, {name: string; enabled: boolean}>,
isSetupCategoriesTaskParentReportArchived: boolean,
setupCategoryTaskReport: OnyxEntry<Report>,
setupCategoryTaskParentReport: OnyxEntry<Report>,
currentUserAccountID: number,
hasOutstandingChildTask: boolean,
parentReportAction: OnyxEntry<ReportAction> | undefined,
) {
function setWorkspaceCategoryEnabled({
policyData,
categoriesToUpdate,
isSetupCategoriesTaskParentReportArchived,
setupCategoryTaskReport,
setupCategoryTaskParentReport,
currentUserAccountID,
hasOutstandingChildTask,
parentReportAction,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
policyHasTags,
}: SetWorkspaceCategoryEnabledParams) {
const policyID = policyData.policy?.id;
const policyCategoriesOptimisticData = {
...Object.keys(categoriesToUpdate).reduce<PolicyCategories>((acc, key) => {
Expand Down Expand Up @@ -414,6 +454,18 @@ function setWorkspaceCategoryEnabled(
parentReportAction,
);

if (setupCategoriesAndTagsTaskReport && policyHasTags) {
appendSetupCategoriesOnboardingData(
onyxData,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived ?? false,
currentUserAccountID,
setupCategoriesAndTagsHasOutstandingChildTask ?? false,
setupCategoriesAndTagsParentReportAction,
);
}

const parameters = {
policyID,
categories: JSON.stringify(Object.keys(categoriesToUpdate).map((key) => categoriesToUpdate[key])),
Expand Down Expand Up @@ -620,16 +672,22 @@ function removePolicyCategoryReceiptsRequired(policyData: PolicyData, categoryNa
API.write(WRITE_COMMANDS.REMOVE_POLICY_CATEGORY_RECEIPTS_REQUIRED, parameters, onyxData);
}

function createPolicyCategory(
policyID: string,
categoryName: string,
isSetupCategoriesTaskParentReportArchived: boolean,
setupCategoryTaskReport: OnyxEntry<Report>,
setupCategoryTaskParentReport: OnyxEntry<Report>,
currentUserAccountID: number,
hasOutstandingChildTask: boolean,
parentReportAction: OnyxEntry<ReportAction>,
) {
function createPolicyCategory({
policyID,
categoryName,
isSetupCategoriesTaskParentReportArchived,
setupCategoryTaskReport,
setupCategoryTaskParentReport,
currentUserAccountID,
hasOutstandingChildTask,
parentReportAction,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived,
setupCategoriesAndTagsHasOutstandingChildTask,
setupCategoriesAndTagsParentReportAction,
policyHasTags,
}: CreatePolicyCategoryParams) {
const onyxData = buildOptimisticPolicyCategories(policyID, [categoryName]);
appendSetupCategoriesOnboardingData(
onyxData,
Expand All @@ -640,6 +698,18 @@ function createPolicyCategory(
hasOutstandingChildTask,
parentReportAction,
);
// Complete the combined "Set up categories and tags" task only if tags already exist
if (setupCategoriesAndTagsTaskReport && policyHasTags) {
appendSetupCategoriesOnboardingData(
onyxData,
setupCategoriesAndTagsTaskReport,
setupCategoriesAndTagsTaskParentReport,
isSetupCategoriesAndTagsTaskParentReportArchived ?? false,
currentUserAccountID,
setupCategoriesAndTagsHasOutstandingChildTask ?? false,
setupCategoriesAndTagsParentReportAction,
);
}
const parameters = {
policyID,
categories: JSON.stringify([{name: categoryName}]),
Expand Down
27 changes: 25 additions & 2 deletions src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@
import {pushTransactionViolationsOnyxData} from '@libs/ReportUtils';
import {getTagArrayFromName} from '@libs/TransactionUtils';
import type {PolicyTagList} from '@pages/workspace/tags/types';
import {completeTask} from '@userActions/Task';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ImportedSpreadsheet, Policy, PolicyTag, PolicyTagLists, PolicyTags, RecentlyUsedTags} from '@src/types/onyx';
import type {ImportedSpreadsheet, Policy, PolicyTag, PolicyTagLists, PolicyTags, RecentlyUsedTags, Report} from '@src/types/onyx';
import type {OnyxValueWithOfflineFeedback} from '@src/types/onyx/OnyxCommon';
import type {ApprovalRule} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';

/**
* Checks if a task report is incomplete (not approved)
*/
function isTaskIncomplete(taskReport: OnyxEntry<Report>): boolean {
return !!taskReport && (taskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || taskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED);
}

let allPolicyTags: OnyxCollection<PolicyTagLists> = {};
Onyx.connect({

Check warning on line 48 in src/libs/actions/Policy/Tag.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
Expand Down Expand Up @@ -126,7 +134,14 @@
return onyxData;
}

function createPolicyTag(policyID: string, tagName: string, policyTags: PolicyTagLists = {}) {
function createPolicyTag(
policyID: string,
tagName: string,
policyTags: PolicyTagLists = {},
setupTagsTaskReport?: OnyxEntry<Report>,
setupCategoriesAndTagsTaskReport?: OnyxEntry<Report>,
policyHasCustomCategories?: boolean,
) {
const policyTag = PolicyUtils.getTagLists(policyTags)?.at(0) ?? ({} as PolicyTagList);
const newTagName = PolicyUtils.escapeTagName(tagName);

Expand Down Expand Up @@ -188,6 +203,14 @@
};

API.write(WRITE_COMMANDS.CREATE_POLICY_TAG, parameters, onyxData);

if (isTaskIncomplete(setupTagsTaskReport)) {
completeTask(setupTagsTaskReport, false, false, undefined);
}

if (isTaskIncomplete(setupCategoriesAndTagsTaskReport) && policyHasCustomCategories) {
completeTask(setupCategoriesAndTagsTaskReport, false, false, undefined);
}
}

function importPolicyTags(policyID: string, tags: PolicyTag[]) {
Expand Down
Loading
Loading