Skip to content

Commit 6516767

Browse files
committed
refactor: minor type edits
1 parent 1a3e61f commit 6516767

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

server/src/permissions/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import {
88
isCreatedBySelf,
99
isSelf,
1010
isToday
11-
} from './utils';
11+
} from '@/permissions/utils';
1212

1313
export const ROLES = {
1414
VOLUNTEER: 'VOLUNTEER',
1515
MANAGER: 'MANAGER',
1616
ADMIN: 'ADMIN',
17-
SUPER_ADMIN: 'SUPER_ADMIN' // super admin de-prioritized for now
17+
SUPER_ADMIN: 'SUPER_ADMIN'
1818
};
1919

2020
// Native CASL actions -- CRUD + master action "manage"
@@ -113,7 +113,8 @@ export type Context = { userObjectId: string; latestLocationObjectId: string };
113113
export type Action = (typeof ACTION_ENUM)[number];
114114
export type Subject = (typeof SUBJECT_ENUM)[number];
115115
export type Condition = (typeof CONDITION_ENUM)[number];
116-
export type Query = ReturnType<(typeof CONDITION_QUERIES)[Condition]> | {}; // @typescript-eslint/no-empty-object-type
116+
117+
export type Query = ReturnType<(typeof CONDITION_QUERIES)[Condition]> | object;
117118
// here, ForcedSubject is a type used when checking auth in our routes (i.e. building a dummy subject intead of just passing in a string)
118119
export type Ability = MongoAbility<
119120
[Action, Subject | ForcedSubject<Subject>],

server/src/permissions/utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
// Returns a Mongo query filter that checks if the value of the given field is today
22
// Made fieldName variable in case we want to have different checks for `updatedAt`/`createdAt`/`deletedAt`/etc.
3-
export function isToday(fieldName: string) {
3+
export function isToday(fieldName: string, timezone: string = 'UTC') {
44
const today = new Date();
55
return {
6-
$expr: {
7-
$eq: [
6+
$expr: { // Allows use of aggregation operators in queries
7+
$eq: [ // Checks equality between two values
8+
// Value 1: The field from the document
89
{
9-
$dateToString: { format: '%Y-%m-%d', date: `$${fieldName}` }
10+
$dateToString: { format: '%Y-%m-%d', date: `$${fieldName}`, timezone }
1011
},
11-
{ $dateToString: { format: '%Y-%m-%d', date: today } }
12+
// Value 2: Today's date
13+
{ $dateToString: { format: '%Y-%m-%d', date: today, timezone } }
1214
]
1315
}
1416
};
1517
}
1618

17-
export function hasSameLocation(locationObjectId: string) {
19+
export function hasSameLocation(locationObjectId: string): { locationObjectId: string } {
1820
return {
19-
locationObjectId: locationObjectId
21+
locationObjectId
2022
};
2123
}
2224

23-
export function isSelf(userObjectId: string) {
25+
export function isSelf(userObjectId: string): { _id: string } {
2426
return {
2527
_id: userObjectId
2628
};
2729
}
2830

29-
export function isCreatedBySelf(userObjectId: string) {
31+
export function isCreatedBySelf(userObjectId: string): { createdByUserObjectId: string } {
3032
return {
3133
createdByUserObjectId: userObjectId
3234
};
3335
}
3436

35-
export function hasRole(roles: string[]) {
37+
export function hasRole(roles: string[]): { role: { $in: string[] } } {
3638
return {
3739
role: { $in: roles }
3840
};

0 commit comments

Comments
 (0)