Skip to content

Commit 589114a

Browse files
authored
Merge pull request #30 from rezo-labs/patch/fix_current_user
Fix $CURRENT_USER when using nested field
2 parents 304a735 + 639f3f7 commit 589114a

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/operations.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@ jest
66
.setSystemTime(new Date('2023-01-01'));
77

88
describe('Test parseExpression', () => {
9-
test('Dynamic variables', () => {
10-
expect(parseExpression('$NOW', {})).toStrictEqual(new Date());
11-
expect(parseExpression('$CURRENT_USER', {})).toBe(undefined);
12-
expect(parseExpression('$CURRENT_USER', { __currentUser: 1 })).toBe(1);
9+
describe('Dynamic variables', () => {
10+
test('$NOW', () => {
11+
expect(parseExpression('$NOW', {})).toStrictEqual(new Date());
12+
});
13+
14+
test('$CURRENT_USER', () => {
15+
const user = {
16+
id: 1,
17+
name: 'Duy',
18+
role: {
19+
name: 'admin',
20+
},
21+
};
22+
expect(parseExpression('$CURRENT_USER', {})).toBe(undefined);
23+
expect(parseExpression('$CURRENT_USER', { __currentUser: user })).toBe(1);
24+
expect(parseExpression('$CURRENT_USER.name', { __currentUser: user })).toBe('Duy');
25+
expect(parseExpression('$CURRENT_USER.role.name', { __currentUser: user })).toBe('admin');
26+
expect(parseExpression('$CURRENT_USER.something', { __currentUser: user })).toBe(null);
27+
});
1328
});
1429

1530
test('INT op', () => {

src/operations.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
1313
if (exp === '$NOW') {
1414
return new Date();
1515
}
16-
if (exp === '$CURRENT_USER') {
17-
return values.__currentUser;
16+
if (exp.startsWith('$CURRENT_USER')) {
17+
if (exp === '$CURRENT_USER') {
18+
return values.__currentUser?.id;
19+
}
20+
return findValueByPath({ $CURRENT_USER: values.__currentUser }, exp).value;
1821
}
1922

2023
const opMatch = parseOp(exp);

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const useDeepValues = (
5555
template: string
5656
) => {
5757
const api = useApi();
58-
const currentUser = useStores().useUserStore().currentUser.id;
58+
const { currentUser } = useStores().useUserStore();
5959
const finalValues = ref<Record<string, any>>({});
6060
let fieldCache: Record<string, any> = {};
6161
let itemCache: Record<string, any> = {};

0 commit comments

Comments
 (0)