Skip to content

Commit

Permalink
feat: allow update of account signatures
Browse files Browse the repository at this point in the history
Update the signature of updateAccount function to let update of identities and signatures in account store.

Co-authored-by: Luca Stauble <[email protected]>
Refs: SHELL-230 CO-1136 (#462)
  • Loading branch information
dhavaldodiya and gnekoz authored Jul 10, 2024
1 parent 993f738 commit fd240f2
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 56 deletions.
42 changes: 33 additions & 9 deletions api-extractor/carbonio-shell-ui.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type Account = {
name: string;
displayName: string;
signatures: {
signature: Array<unknown>;
signature: Array<Signature>;
};
identities: {
identity: Array<Identity>;
Expand Down Expand Up @@ -1300,6 +1300,18 @@ export type SettingsViewProps = {};
// @public (undocumented)
export const SHELL_APP_ID = "carbonio-shell-ui";

// @public (undocumented)
type Signature = {
name: string;
id: string;
content?: [
{
type: 'text/plain' | 'text/html';
_content: string;
}
];
};

// @public (undocumented)
export type SoapBody<TBody = Record<string, unknown>> = TBody & {
_jsns: NameSpace;
Expand Down Expand Up @@ -1466,14 +1478,25 @@ export class Tracker {
trackPageView(customTitle?: string): void;
}

// Warning: (ae-forgotten-export) The symbol "UpdateAccountParams" needs to be exported by the entry point lib.d.ts
//
// @public (undocumented)
type UpdateAccount = (accountMods: IdentityMods, identities: Identity[]) => void;
type UpdateAccount = (accountMods: UpdateAccountParams) => void;

// Warning: (ae-forgotten-export) The symbol "UpdateAccount" needs to be exported by the entry point lib.d.ts
//
// @public (undocumented)
export const updateAccount: UpdateAccount;

// @public (undocumented)
type UpdateAccountParams = {
identities?: {
identitiesMods: IdentityMods;
newIdentities: Identity[];
};
signatures?: Signature[];
};

// @public (undocumented)
export const updateBoard: <T = unknown>(id: string, board: Partial<Board<T>>) => void;

Expand Down Expand Up @@ -1708,13 +1731,14 @@ interface ZimletProp {
// lib/store/context-bridge.d.ts:5:5 - (ae-forgotten-export) The symbol "AnyFunction" needs to be exported by the entry point lib.d.ts
// lib/store/integrations/store.d.ts:26:9 - (ae-forgotten-export) The symbol "Action_2" needs to be exported by the entry point lib.d.ts
// lib/store/integrations/store.d.ts:32:9 - (ae-forgotten-export) The symbol "Component" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:33:5 - (ae-forgotten-export) The symbol "AccountRights" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:37:5 - (ae-forgotten-export) The symbol "StringOfLength" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:85:5 - (ae-forgotten-export) The symbol "AccountSettingsAttrs" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:87:5 - (ae-forgotten-export) The symbol "ZimletProp" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:131:5 - (ae-forgotten-export) The symbol "AccountRightTargetEmail" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:136:9 - (ae-forgotten-export) The symbol "AccountRightName" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:137:9 - (ae-forgotten-export) The symbol "AccountRightTarget" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:38:9 - (ae-forgotten-export) The symbol "Signature" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:43:5 - (ae-forgotten-export) The symbol "AccountRights" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:47:5 - (ae-forgotten-export) The symbol "StringOfLength" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:95:5 - (ae-forgotten-export) The symbol "AccountSettingsAttrs" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:97:5 - (ae-forgotten-export) The symbol "ZimletProp" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:141:5 - (ae-forgotten-export) The symbol "AccountRightTargetEmail" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:146:9 - (ae-forgotten-export) The symbol "AccountRightName" needs to be exported by the entry point lib.d.ts
// lib/types/account/index.d.ts:147:9 - (ae-forgotten-export) The symbol "AccountRightTarget" needs to be exported by the entry point lib.d.ts
// lib/types/apps/index.d.ts:66:5 - (ae-forgotten-export) The symbol "PanelMode" needs to be exported by the entry point lib.d.ts
// lib/types/misc/index.d.ts:85:9 - (ae-forgotten-export) The symbol "SoapPolicy" needs to be exported by the entry point lib.d.ts
// lib/types/misc/index.d.ts:104:5 - (ae-forgotten-export) The symbol "FolderView" needs to be exported by the entry point lib.d.ts
Expand Down
151 changes: 149 additions & 2 deletions src/store/account/updaters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { faker } from '@faker-js/faker';
import { times } from 'lodash';

import { useAccountStore } from './store';
import { updateSettings } from './updaters';
import { updateAccount, updateSettings } from './updaters';
import { setupAccountStore } from '../../tests/account-utils';
import type { Identity, Signature } from '../../types/account';

describe('updateSettings', () => {
it('should leave the state settings unchanged if no mods are set', () => {
it('should leave the settings state unchanged if no mods are set', () => {
setupAccountStore();
const prevSettings = useAccountStore.getState().settings;
updateSettings({});
Expand Down Expand Up @@ -68,3 +72,146 @@ describe('updateSettings', () => {
);
});
});

describe('updateAccount', () => {
it('should leave the account state unchanged if no mods are set', () => {
setupAccountStore();
const prevState = useAccountStore.getState().account;
updateAccount({});
expect(useAccountStore.getState().account).toEqual(prevState);
});

it('should change the displayName of the account if the identity of the primary account is changed', () => {
setupAccountStore();
const prevState = useAccountStore.getState().account;
if (!prevState) {
throw new Error('Account not found in the store');
}

const newIdentityName = faker.person.fullName();
updateAccount({
identities: {
identitiesMods: {
modifyList: {
[prevState.id]: {
id: prevState.id,
prefs: {
zimbraPrefIdentityName: newIdentityName
}
}
}
},
newIdentities: []
}
});
expect(useAccountStore.getState().account?.displayName).toEqual(newIdentityName);
});

it('should update the store with a new identity', () => {
setupAccountStore();
const prevState = useAccountStore.getState().account;
if (!prevState) {
throw new Error('Account not found in the store');
}

const newIdentity: Identity = {
id: faker.string.uuid(),
name: faker.person.fullName(),
_attrs: {}
};

updateAccount({
identities: {
identitiesMods: {},
newIdentities: [newIdentity]
}
});

expect(useAccountStore.getState().account?.identities.identity).toHaveLength(
prevState.identities.identity.length + 1
);
});

it('should update the store with the given signatures', () => {
setupAccountStore();
const prevState = useAccountStore.getState().account;
if (!prevState) {
throw new Error('Account not found in the store');
}

const signatures: Array<Signature> = times(
5,
(): Signature => ({
id: faker.string.uuid(),
name: faker.word.noun(),
content: [
{
type: 'text/html',
_content: `<p>${faker.person.fullName()}</p>`
}
]
})
);

updateAccount({
signatures
});

expect(useAccountStore.getState().account?.signatures.signature).toEqual(signatures);
});

it('updates in signatures should not change the identities in the store', () => {
setupAccountStore();
const prevState = useAccountStore.getState().account;
if (!prevState) {
throw new Error('Account not found in the store');
}

const signatures: Array<Signature> = times(
5,
(): Signature => ({
id: faker.string.uuid(),
name: faker.word.noun(),
content: [
{
type: 'text/html',
_content: `<p>${faker.person.fullName()}</p>`
}
]
})
);

updateAccount({
signatures
});

expect(useAccountStore.getState().account?.identities.identity).toEqual(
prevState.identities.identity
);
});

it('updates in identities should not change the signatures in the store', () => {
setupAccountStore();
const prevState = useAccountStore.getState().account;
if (!prevState) {
throw new Error('Account not found in the store');
}

const newIdentity: Identity = {
id: faker.string.uuid(),
name: faker.person.fullName(),
_attrs: {}
};

updateAccount({
identities: {
identitiesMods: {},
newIdentities: [newIdentity]
}
});

expect(useAccountStore.getState().account?.signatures.signature).toEqual(
prevState.signatures.signature
);
});
});
44 changes: 35 additions & 9 deletions src/store/account/updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { find } from 'lodash';

import { useAccountStore } from './store';
import { mergeAttrs, mergePrefs, mergeProps, updateIdentities } from './utils';
import type { AccountSettingsAttrs, AccountSettingsPrefs, Identity } from '../../types/account';
import type {
AccountSettingsAttrs,
AccountSettingsPrefs,
Identity,
Signature
} from '../../types/account';
import type { IdentityMods } from '../../types/network';

type UpdateSettingsParams = {
Expand All @@ -17,7 +22,16 @@ type UpdateSettingsParams = {
};

export type UpdateSettings = (settingsMods: UpdateSettingsParams) => void;
export type UpdateAccount = (accountMods: IdentityMods, identities: Identity[]) => void;

type UpdateAccountParams = {
identities?: {
identitiesMods: IdentityMods;
newIdentities: Identity[];
};
signatures?: Signature[];
};

export type UpdateAccount = (accountMods: UpdateAccountParams) => void;

export const updateSettings: UpdateSettings = (settingsMods) =>
useAccountStore.setState((state) => ({
Expand All @@ -29,19 +43,31 @@ export const updateSettings: UpdateSettings = (settingsMods) =>
}
}));

export const updateAccount: UpdateAccount = (identityMods, newIdentities) =>
export const updateAccount: UpdateAccount = ({ identities, signatures }) =>
useAccountStore.setState((state) =>
state.account
? {
...state,
account: {
...state.account,
displayName:
find(identityMods?.modifyList, (item) => item.id === state?.account?.id)?.prefs
.zimbraPrefIdentityName ?? state.account?.displayName,
identities: {
identity: updateIdentities(state, identityMods, newIdentities) ?? []
}
...(identities
? {
displayName:
find(
identities?.identitiesMods?.modifyList,
(item) => item.id === state?.account?.id
)?.prefs.zimbraPrefIdentityName ?? state.account?.displayName,
identities: {
identity:
updateIdentities(
state,
identities.identitiesMods,
identities.newIdentities
) ?? []
}
}
: {}),
...(signatures ? { signatures: { signature: signatures } } : {})
}
}
: state
Expand Down
Loading

0 comments on commit fd240f2

Please sign in to comment.