-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.ts
72 lines (66 loc) · 2.73 KB
/
Resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import BaseEntity from "./BaseEntity";
import { isEqual } from 'lodash';
/**
* A class representing a {@link Resource} on the broker.
*/
export class Resource extends BaseEntity {
/**
* The Id of the {@link User} that owns this {@link Resource}.
*/
public userId: string;
/**
* The Id of the {@link Client} application that the {@link Resource} belongs to.
*/
public clientId: string;
/**
* The Id of the {@link User}s with whom this {@link Resource} is shared.
*/
public sharedWithIds: string[];
/**
* The name of the {@link Resource}.
*/
public name: string;
/**
* Whether this is the default {@link Resource} of a given {@link User} and {@link Client} combination.
* True if it is, false if it isn't.
*/
public default: boolean;
/**
* The unstructured data that
*/
public data: any;
/**
* A constructor that creates a {@link Resource} object from a plain client object received from the broker.
* @param resource - A plain object received from the broker with information about a {@link Resource}.
*/
constructor(resource: any = {}) {
super(resource);
}
/**
* It updates the current object with information from a plain object received from the broker.
* @param resource - A plain object received from the broker with information about a {@link Resource}.
*/
public update(resource: any): void {
super.update(resource);
this.userId = resource.user && resource.user._id ? resource.user._id : resource.user;
this.clientId = resource.client && resource.client._id ? resource.client._id : resource.client;
this.sharedWithIds = resource.sharedWith ? resource.sharedWith.map((u: any) => u._id ? u._id : u) : [];
this.name = resource.name;
this.default = resource.default;
this.data = resource.data;
}
/**
* A method that compares the current object with another object to determine if their values are equivalent.
* @param resource - The object to compare to.
*/
public equals(resource: any): boolean {
return super.equals(resource)
&& this.userId === (resource.userId || resource.user || resource.user.id || resource.user._id)
&& this.clientId === (resource.clientId || resource.client || resource.client.id || resource.client._id)
&& (isEqual(this.sharedWithIds, resource.sharedWith) || isEqual(this.sharedWithIds, resource.sharedWithIds))
&& this.name === resource.name
&& this.default === resource.default
&& isEqual(this.data, resource.data)
}
}
export default Resource;