diff --git a/src/operations.test.ts b/src/operations.test.ts index 9ab48d77..a5691643 100644 --- a/src/operations.test.ts +++ b/src/operations.test.ts @@ -6,10 +6,25 @@ jest .setSystemTime(new Date('2023-01-01')); describe('Test parseExpression', () => { - test('Dynamic variables', () => { - expect(parseExpression('$NOW', {})).toStrictEqual(new Date()); - expect(parseExpression('$CURRENT_USER', {})).toBe(undefined); - expect(parseExpression('$CURRENT_USER', { __currentUser: 1 })).toBe(1); + describe('Dynamic variables', () => { + test('$NOW', () => { + expect(parseExpression('$NOW', {})).toStrictEqual(new Date()); + }); + + test('$CURRENT_USER', () => { + const user = { + id: 1, + name: 'Duy', + role: { + name: 'admin', + }, + }; + expect(parseExpression('$CURRENT_USER', {})).toBe(undefined); + expect(parseExpression('$CURRENT_USER', { __currentUser: user })).toBe(1); + expect(parseExpression('$CURRENT_USER.name', { __currentUser: user })).toBe('Duy'); + expect(parseExpression('$CURRENT_USER.role.name', { __currentUser: user })).toBe('admin'); + expect(parseExpression('$CURRENT_USER.something', { __currentUser: user })).toBe(null); + }); }); test('INT op', () => { diff --git a/src/operations.ts b/src/operations.ts index ca685a14..f80e1e09 100644 --- a/src/operations.ts +++ b/src/operations.ts @@ -13,8 +13,11 @@ export function parseExpression(exp: string, values: Record): any { if (exp === '$NOW') { return new Date(); } - if (exp === '$CURRENT_USER') { - return values.__currentUser; + if (exp.startsWith('$CURRENT_USER')) { + if (exp === '$CURRENT_USER') { + return values.__currentUser?.id; + } + return findValueByPath({ $CURRENT_USER: values.__currentUser }, exp).value; } const opMatch = parseOp(exp); diff --git a/src/utils.ts b/src/utils.ts index 29e61a30..3e961ce9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -55,7 +55,7 @@ export const useDeepValues = ( template: string ) => { const api = useApi(); - const currentUser = useStores().useUserStore().currentUser.id; + const { currentUser } = useStores().useUserStore(); const finalValues = ref>({}); let fieldCache: Record = {}; let itemCache: Record = {};