Skip to content
39 changes: 35 additions & 4 deletions src/libs/actions/Domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,12 @@ function clearToggleConsolidatedDomainBillingErrors(domainAccountID: number) {
});
}

function addAdminToDomain(domainAccountID: number, accountID: number, targetEmail: string, domainName: string) {
function addAdminToDomain(domainAccountID: number, accountID: number, targetEmail: string, domainName: string, isOptimisticAccount: boolean) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make sure to cover these changes in the unit tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mountiny I updated my PR.

const PERMISSION_KEY = `${CONST.DOMAIN.EXPENSIFY_ADMIN_ACCESS_PREFIX}${accountID}`;

const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.DOMAIN | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS>> = [
const optimisticData: Array<
OnyxUpdate<typeof ONYXKEYS.COLLECTION.DOMAIN | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS | typeof ONYXKEYS.PERSONAL_DETAILS_LIST>
> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.DOMAIN}${domainAccountID}`,
Expand Down Expand Up @@ -575,7 +577,24 @@ function addAdminToDomain(domainAccountID: number, accountID: number, targetEmai
},
];

const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.DOMAIN | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS>> = [
if (isOptimisticAccount) {
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.PERSONAL_DETAILS_LIST}`,
value: {
[accountID]: {
accountID,
login: targetEmail,
displayName: targetEmail,
isOptimisticPersonalDetail: true,
},
},
Comment thread
mountiny marked this conversation as resolved.
});
}

const successData: Array<
OnyxUpdate<typeof ONYXKEYS.COLLECTION.DOMAIN | typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS | typeof ONYXKEYS.PERSONAL_DETAILS_LIST>
> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.DOMAIN}${domainAccountID}`,
Expand Down Expand Up @@ -605,7 +624,7 @@ function addAdminToDomain(domainAccountID: number, accountID: number, targetEmai
},
];

const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS>> = [
const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.DOMAIN_PENDING_ACTIONS | typeof ONYXKEYS.COLLECTION.DOMAIN_ERRORS | typeof ONYXKEYS.PERSONAL_DETAILS_LIST>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.DOMAIN_ERRORS}${domainAccountID}`,
Expand All @@ -628,6 +647,18 @@ function addAdminToDomain(domainAccountID: number, accountID: number, targetEmai
},
];

if (isOptimisticAccount) {
const clearOptimisticPersonalDetails: OnyxUpdate<typeof ONYXKEYS.PERSONAL_DETAILS_LIST> = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.PERSONAL_DETAILS_LIST}`,
value: {
[accountID]: null,
},
};
successData.push(clearOptimisticPersonalDetails);
failureData.push(clearOptimisticPersonalDetails);
}

const params: AddAdminToDomainParams = {
domainName,
targetEmail,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/domain/Admins/DomainAddAdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function DomainAddAdminPage({route}: DomainAddAdminProps) {
}
didInvite.current = true;

addAdminToDomain(domainAccountID, currentlySelectedUser.accountID, currentlySelectedUser.login, domainName);
addAdminToDomain(domainAccountID, currentlySelectedUser.accountID, currentlySelectedUser.login, domainName, !!currentlySelectedUser.isOptimisticAccount);
Navigation.dismissModal();
};

Expand Down
70 changes: 70 additions & 0 deletions tests/actions/DomainTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Onyx from 'react-native-onyx';
import {
addAdminToDomain,
addMemberToDomain,
clearDomainErrors,
clearDomainMemberError,
Expand Down Expand Up @@ -187,6 +188,75 @@ describe('actions/Domain', () => {
apiWriteSpy.mockRestore();
});

it('addAdminToDomain - adds and clears optimistic personal details for optimistic accounts', () => {
const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve());
const domainAccountID = 123;
const accountID = 456;
const targetEmail = 'test@example.com';
const domainName = 'test.com';

addAdminToDomain(domainAccountID, accountID, targetEmail, domainName, true);

expect(apiWriteSpy).toHaveBeenCalledWith(
WRITE_COMMANDS.ADD_DOMAIN_ADMIN,
{domainName, targetEmail},
{
optimisticData: expect.arrayContaining([
expect.objectContaining({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[accountID]: {
accountID,
login: targetEmail,
displayName: targetEmail,
isOptimisticPersonalDetail: true,
},
},
}),
]),
successData: expect.arrayContaining([
expect.objectContaining({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {[accountID]: null},
}),
]),
failureData: expect.arrayContaining([
expect.objectContaining({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {[accountID]: null},
}),
]),
},
);

apiWriteSpy.mockRestore();
});

it('addAdminToDomain - does not update optimistic personal details for non-optimistic accounts', () => {
const apiWriteSpy = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve());
const domainAccountID = 123;
const accountID = 456;
const targetEmail = 'test@example.com';
const domainName = 'test.com';

addAdminToDomain(domainAccountID, accountID, targetEmail, domainName, false);

expect(apiWriteSpy).toHaveBeenCalledWith(
WRITE_COMMANDS.ADD_DOMAIN_ADMIN,
{domainName, targetEmail},
expect.objectContaining({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
optimisticData: expect.not.arrayContaining([expect.objectContaining({key: ONYXKEYS.PERSONAL_DETAILS_LIST})]),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
successData: expect.not.arrayContaining([expect.objectContaining({key: ONYXKEYS.PERSONAL_DETAILS_LIST})]),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
failureData: expect.not.arrayContaining([expect.objectContaining({key: ONYXKEYS.PERSONAL_DETAILS_LIST})]),
}),
);

apiWriteSpy.mockRestore();
});

it('clearAddMemberError - clears member errors and optimistic data', async () => {
const domainAccountID = 123;
const email = 'test@example.com';
Expand Down
Loading