Skip to content

Commit

Permalink
Merge pull request #30 from rezo-labs/patch/fix_current_user
Browse files Browse the repository at this point in the history
Fix $CURRENT_USER when using nested field
  • Loading branch information
duydvu authored Dec 29, 2022
2 parents 304a735 + 639f3f7 commit 589114a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
23 changes: 19 additions & 4 deletions src/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export function parseExpression(exp: string, values: Record<string, any>): 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);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, any>>({});
let fieldCache: Record<string, any> = {};
let itemCache: Record<string, any> = {};
Expand Down

0 comments on commit 589114a

Please sign in to comment.