@temboplus/afloat 0.1.13 → 0.1.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/src/features/auth/access/contract.d.ts +14 -0
- package/esm/src/features/auth/access/contract.d.ts.map +1 -0
- package/esm/src/features/auth/access/contract.js +14 -0
- package/esm/src/features/auth/contract.d.ts +20 -20
- package/esm/src/features/auth/contract.js +2 -2
- package/esm/src/features/auth/identity/contract.d.ts +0 -7
- package/esm/src/features/auth/identity/contract.d.ts.map +1 -1
- package/esm/src/features/auth/identity/contract.js +0 -1
- package/esm/src/features/auth/identity/repository.d.ts +2 -2
- package/esm/src/features/auth/identity/repository.d.ts.map +1 -1
- package/esm/src/features/auth/identity/repository.js +5 -5
- package/esm/src/features/auth/manager.js +1 -1
- package/esm/src/features/auth/profile/contract.d.ts +17 -25
- package/esm/src/features/auth/profile/contract.d.ts.map +1 -1
- package/esm/src/features/auth/profile/contract.js +2 -4
- package/esm/src/features/auth/repository.d.ts +0 -1
- package/esm/src/features/auth/repository.d.ts.map +1 -1
- package/esm/src/features/auth/repository.js +3 -11
- package/esm/src/features/auth/storage/server_token_handler.d.ts +7 -6
- package/esm/src/features/auth/storage/server_token_handler.d.ts.map +1 -1
- package/esm/src/features/auth/storage/server_token_handler.js +73 -26
- package/esm/src/features/contact/contract.d.ts +10 -10
- package/esm/src/features/payout/contract.d.ts +12 -12
- package/esm/src/features/wallet/repository.d.ts +1 -28
- package/esm/src/features/wallet/repository.d.ts.map +1 -1
- package/esm/src/features/wallet/repository.js +0 -51
- package/esm/src/models/contact/derivatives/contact.js +2 -2
- package/esm/src/models/contact/derivatives/contact_info.d.ts +8 -8
- package/esm/src/models/contact/derivatives/contact_info.d.ts.map +1 -1
- package/esm/src/models/contact/derivatives/contact_info.js +12 -12
- package/esm/src/models/payout/channel.d.ts +4 -4
- package/esm/src/models/payout/channel.d.ts.map +1 -1
- package/esm/src/models/payout/channel.js +2 -2
- package/esm/src/models/payout/derivatives/payout.d.ts.map +1 -1
- package/esm/src/models/payout/derivatives/payout.js +2 -2
- package/esm/src/models/user/profile.d.ts +135 -12
- package/esm/src/models/user/profile.d.ts.map +1 -1
- package/esm/src/models/user/profile.js +318 -12
- package/esm/src/models/user/user.d.ts +14 -17
- package/esm/src/models/user/user.d.ts.map +1 -1
- package/esm/src/models/user/user.js +109 -26
- package/esm/src/models/wallet/index.d.ts +0 -1
- package/esm/src/models/wallet/index.d.ts.map +1 -1
- package/esm/src/models/wallet/index.js +0 -1
- package/esm/src/shared/token_required_repository.d.ts +78 -0
- package/esm/src/shared/token_required_repository.d.ts.map +1 -0
- package/esm/src/shared/token_required_repository.js +128 -0
- package/package.json +7 -7
- package/script/src/features/auth/access/contract.d.ts +14 -0
- package/script/src/features/auth/access/contract.d.ts.map +1 -0
- package/script/src/features/auth/access/contract.js +17 -0
- package/script/src/features/auth/contract.d.ts +20 -20
- package/script/src/features/auth/contract.js +1 -1
- package/script/src/features/auth/identity/contract.d.ts +0 -7
- package/script/src/features/auth/identity/contract.d.ts.map +1 -1
- package/script/src/features/auth/identity/contract.js +0 -1
- package/script/src/features/auth/identity/repository.d.ts +2 -2
- package/script/src/features/auth/identity/repository.d.ts.map +1 -1
- package/script/src/features/auth/identity/repository.js +5 -5
- package/script/src/features/auth/manager.js +1 -1
- package/script/src/features/auth/profile/contract.d.ts +17 -25
- package/script/src/features/auth/profile/contract.d.ts.map +1 -1
- package/script/src/features/auth/profile/contract.js +1 -3
- package/script/src/features/auth/repository.d.ts +0 -1
- package/script/src/features/auth/repository.d.ts.map +1 -1
- package/script/src/features/auth/repository.js +3 -11
- package/script/src/features/auth/storage/server_token_handler.d.ts +7 -6
- package/script/src/features/auth/storage/server_token_handler.d.ts.map +1 -1
- package/script/src/features/auth/storage/server_token_handler.js +72 -25
- package/script/src/features/contact/contract.d.ts +10 -10
- package/script/src/features/payout/contract.d.ts +12 -12
- package/script/src/features/wallet/repository.d.ts +1 -28
- package/script/src/features/wallet/repository.d.ts.map +1 -1
- package/script/src/features/wallet/repository.js +0 -51
- package/script/src/models/contact/derivatives/contact.js +1 -1
- package/script/src/models/contact/derivatives/contact_info.d.ts +8 -8
- package/script/src/models/contact/derivatives/contact_info.d.ts.map +1 -1
- package/script/src/models/contact/derivatives/contact_info.js +11 -11
- package/script/src/models/payout/channel.d.ts +4 -4
- package/script/src/models/payout/channel.d.ts.map +1 -1
- package/script/src/models/payout/channel.js +2 -2
- package/script/src/models/payout/derivatives/payout.d.ts.map +1 -1
- package/script/src/models/payout/derivatives/payout.js +1 -1
- package/script/src/models/user/profile.d.ts +135 -12
- package/script/src/models/user/profile.d.ts.map +1 -1
- package/script/src/models/user/profile.js +320 -13
- package/script/src/models/user/user.d.ts +14 -17
- package/script/src/models/user/user.d.ts.map +1 -1
- package/script/src/models/user/user.js +109 -26
- package/script/src/models/wallet/index.d.ts +0 -1
- package/script/src/models/wallet/index.d.ts.map +1 -1
- package/script/src/models/wallet/index.js +0 -1
- package/script/src/shared/token_required_repository.d.ts +78 -0
- package/script/src/shared/token_required_repository.d.ts.map +1 -0
- package/script/src/shared/token_required_repository.js +132 -0
- package/esm/src/features/auth/profile/repository.d.ts +0 -11
- package/esm/src/features/auth/profile/repository.d.ts.map +0 -1
- package/esm/src/features/auth/profile/repository.js +0 -21
- package/esm/src/features/files-gen/contract.d.ts +0 -67
- package/esm/src/features/files-gen/contract.d.ts.map +0 -1
- package/esm/src/features/files-gen/contract.js +0 -40
- package/esm/src/features/files-gen/repository.d.ts +0 -50
- package/esm/src/features/files-gen/repository.d.ts.map +0 -1
- package/esm/src/features/files-gen/repository.js +0 -56
- package/esm/src/models/wallet/statement.d.ts +0 -27
- package/esm/src/models/wallet/statement.d.ts.map +0 -1
- package/esm/src/models/wallet/statement.js +0 -13
- package/script/src/features/auth/profile/repository.d.ts +0 -11
- package/script/src/features/auth/profile/repository.d.ts.map +0 -1
- package/script/src/features/auth/profile/repository.js +0 -25
- package/script/src/features/files-gen/contract.d.ts +0 -67
- package/script/src/features/files-gen/contract.d.ts.map +0 -1
- package/script/src/features/files-gen/contract.js +0 -43
- package/script/src/features/files-gen/repository.d.ts +0 -50
- package/script/src/features/files-gen/repository.d.ts.map +0 -1
- package/script/src/features/files-gen/repository.js +0 -60
- package/script/src/models/wallet/statement.d.ts +0 -27
- package/script/src/models/wallet/statement.d.ts.map +0 -1
- package/script/src/models/wallet/statement.js +0 -16
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { PhoneNumber } from "@temboplus/frontend-core";
|
|
2
3
|
/**
|
|
3
4
|
* Zod schema for validating user profile data.
|
|
4
5
|
* Defines validation rules and constraints for each profile field.
|
|
@@ -6,20 +7,325 @@ import { z } from "zod";
|
|
|
6
7
|
* @const {ProfileType}
|
|
7
8
|
*
|
|
8
9
|
* @property {string} id - Unique identifier for the profile
|
|
9
|
-
* @property {string} firstName - User's first name
|
|
10
|
-
* @property {string} lastName - User's last name
|
|
10
|
+
* @property {string | null | undefined} firstName - User's first name, can be null or undefined
|
|
11
|
+
* @property {string | null | undefined} lastName - User's last name, can be null or undefined
|
|
11
12
|
* @property {string} displayName - User's display name
|
|
12
|
-
* @property {string} phone - User's contact phone number
|
|
13
|
+
* @property {string | null | undefined} phone - User's contact phone number, can be null or undefined
|
|
13
14
|
* @property {string} accountNo - User's account number
|
|
14
|
-
* @property {string} email - User's email address
|
|
15
|
+
* @property {string | null | undefined} email - User's email address, can be null or undefined
|
|
15
16
|
*/
|
|
16
17
|
export const profileSchema = z.object({
|
|
17
|
-
id: z.string()
|
|
18
|
-
firstName: z.string().
|
|
19
|
-
lastName: z.string().
|
|
20
|
-
displayName: z.string()
|
|
21
|
-
phone: z.string().
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
email: z.string().email("Invalid email format"),
|
|
18
|
+
id: z.string(),
|
|
19
|
+
firstName: z.string().optional().nullable(),
|
|
20
|
+
lastName: z.string().optional().nullable(),
|
|
21
|
+
displayName: z.string(),
|
|
22
|
+
phone: z.string().optional().nullable(),
|
|
23
|
+
accountNo: z.string().min(1),
|
|
24
|
+
email: z.string().email().optional().nullable(),
|
|
25
25
|
});
|
|
26
|
+
/**
|
|
27
|
+
* Represents a user profile in the system.
|
|
28
|
+
*
|
|
29
|
+
* This class provides methods for creating, validating, and manipulating user profile data.
|
|
30
|
+
* It integrates with the Zod schema validation for data integrity.
|
|
31
|
+
*/
|
|
32
|
+
export class Profile {
|
|
33
|
+
/**
|
|
34
|
+
* Gets the profile schema used for validation.
|
|
35
|
+
*/
|
|
36
|
+
static get schema() {
|
|
37
|
+
return profileSchema;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new Profile instance with the provided data.
|
|
41
|
+
*
|
|
42
|
+
* Private constructor to enforce use of static factory methods.
|
|
43
|
+
*
|
|
44
|
+
* @param data - Object containing profile information
|
|
45
|
+
*/
|
|
46
|
+
constructor(data) {
|
|
47
|
+
/** Unique identifier for the profile */
|
|
48
|
+
Object.defineProperty(this, "_id", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
writable: true,
|
|
52
|
+
value: void 0
|
|
53
|
+
});
|
|
54
|
+
/** User's first name */
|
|
55
|
+
Object.defineProperty(this, "_firstName", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: void 0
|
|
60
|
+
});
|
|
61
|
+
/** User's last name */
|
|
62
|
+
Object.defineProperty(this, "_lastName", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
/** User's display name, can be used for presentation */
|
|
69
|
+
Object.defineProperty(this, "_displayName", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: void 0
|
|
74
|
+
});
|
|
75
|
+
/** User's phone number, stored as a PhoneNumber object */
|
|
76
|
+
Object.defineProperty(this, "_phone", {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
writable: true,
|
|
80
|
+
value: void 0
|
|
81
|
+
});
|
|
82
|
+
/** User's account number */
|
|
83
|
+
Object.defineProperty(this, "_accountNo", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: void 0
|
|
88
|
+
});
|
|
89
|
+
/** User's email address */
|
|
90
|
+
Object.defineProperty(this, "_email", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: true,
|
|
93
|
+
writable: true,
|
|
94
|
+
value: void 0
|
|
95
|
+
});
|
|
96
|
+
this._id = data.id;
|
|
97
|
+
this._firstName = data.firstName;
|
|
98
|
+
this._lastName = data.lastName;
|
|
99
|
+
this._displayName = data.displayName;
|
|
100
|
+
this._phone = data.phone;
|
|
101
|
+
this._accountNo = data.accountNo;
|
|
102
|
+
this._email = data.email;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Creates a new Profile instance with the provided data.
|
|
106
|
+
*
|
|
107
|
+
* @param data - Object containing profile information.
|
|
108
|
+
*/
|
|
109
|
+
static create(data) {
|
|
110
|
+
let phoneObj = undefined;
|
|
111
|
+
if (data.phone !== undefined && data.phone !== null) {
|
|
112
|
+
phoneObj = PhoneNumber.from(data.phone);
|
|
113
|
+
if (!phoneObj) {
|
|
114
|
+
console.error("Failed to parse phone number:", data.phone);
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
phoneObj = data.phone; // Preserve null or undefined
|
|
120
|
+
}
|
|
121
|
+
return new Profile({
|
|
122
|
+
id: data.id,
|
|
123
|
+
firstName: data.firstName,
|
|
124
|
+
lastName: data.lastName,
|
|
125
|
+
displayName: data.displayName,
|
|
126
|
+
phone: phoneObj,
|
|
127
|
+
accountNo: data.accountNo,
|
|
128
|
+
email: data.email,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Gets the profile's unique identifier.
|
|
133
|
+
*/
|
|
134
|
+
get id() {
|
|
135
|
+
return this._id;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Gets the user's first name.
|
|
139
|
+
*/
|
|
140
|
+
get firstName() {
|
|
141
|
+
return this._firstName;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Gets the user's last name.
|
|
145
|
+
*/
|
|
146
|
+
get lastName() {
|
|
147
|
+
return this._lastName;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Gets the user's display name.
|
|
151
|
+
*/
|
|
152
|
+
get displayName() {
|
|
153
|
+
return this._displayName;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Gets the user's phone number object.
|
|
157
|
+
*/
|
|
158
|
+
get phone() {
|
|
159
|
+
return this._phone;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Gets the user's account number.
|
|
163
|
+
*/
|
|
164
|
+
get accountNo() {
|
|
165
|
+
return this._accountNo;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Gets the user's email address.
|
|
169
|
+
*/
|
|
170
|
+
get email() {
|
|
171
|
+
return this._email;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Gets the user's formatted phone number in international format.
|
|
175
|
+
*/
|
|
176
|
+
get formattedPhone() {
|
|
177
|
+
if (this._phone === undefined) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
return this._phone?.label ?? null;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Gets the user's name for display purposes.
|
|
184
|
+
* Returns the display name if it exists, otherwise returns the first and last name combined.
|
|
185
|
+
*/
|
|
186
|
+
getName() {
|
|
187
|
+
if (this._displayName && this._displayName.trim() !== "") {
|
|
188
|
+
return this._displayName;
|
|
189
|
+
}
|
|
190
|
+
const firstName = this._firstName ?? "";
|
|
191
|
+
const lastName = this._lastName ?? "";
|
|
192
|
+
return `${firstName} ${lastName}`.trim();
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Creates a plain object representation of the profile for validation or serialization.
|
|
196
|
+
*
|
|
197
|
+
* @returns A plain object matching the ProfileType interface
|
|
198
|
+
*/
|
|
199
|
+
toObject() {
|
|
200
|
+
// Handle the phone specially to ensure we preserve undefined vs null
|
|
201
|
+
let phoneString = undefined;
|
|
202
|
+
if (this._phone !== undefined) {
|
|
203
|
+
phoneString = this._phone?.label ?? null;
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
id: this._id,
|
|
207
|
+
firstName: this._firstName,
|
|
208
|
+
lastName: this._lastName,
|
|
209
|
+
displayName: this._displayName,
|
|
210
|
+
phone: phoneString,
|
|
211
|
+
accountNo: this._accountNo,
|
|
212
|
+
email: this._email,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Converts the profile to a JSON string.
|
|
217
|
+
*
|
|
218
|
+
* @returns A JSON string representation of the profile
|
|
219
|
+
*/
|
|
220
|
+
toJSON() {
|
|
221
|
+
return JSON.stringify(this.toObject());
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Validates the profile data against the Zod schema.
|
|
225
|
+
*
|
|
226
|
+
* @returns True if the profile is valid, false otherwise
|
|
227
|
+
*/
|
|
228
|
+
validate() {
|
|
229
|
+
try {
|
|
230
|
+
const result = Profile.schema.safeParse(this.toObject());
|
|
231
|
+
return result.success;
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
console.error("Profile validation error:", error);
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Creates a Profile instance from a JSON string.
|
|
240
|
+
*
|
|
241
|
+
* @param jsonString - JSON string containing profile data
|
|
242
|
+
* @returns A new Profile instance, or undefined if parsing failed
|
|
243
|
+
*/
|
|
244
|
+
static fromJSON(jsonString) {
|
|
245
|
+
try {
|
|
246
|
+
const data = JSON.parse(jsonString);
|
|
247
|
+
return Profile.from(data);
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
console.error("Error parsing profile JSON:", error);
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Creates a Profile instance from a plain object.
|
|
256
|
+
*
|
|
257
|
+
* @param data - Object containing profile data
|
|
258
|
+
* @returns A new Profile instance, or undefined if parsing failed
|
|
259
|
+
*/
|
|
260
|
+
// deno-lint-ignore no-explicit-any
|
|
261
|
+
static from(data) {
|
|
262
|
+
try {
|
|
263
|
+
if (!data) {
|
|
264
|
+
console.error("Data is null or undefined");
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
if (typeof data !== "object") {
|
|
268
|
+
console.error("Data is not an object");
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
if (!data.id || !data.accountNo || !data.displayName) {
|
|
272
|
+
console.error("Missing required profile fields");
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
return Profile.create({
|
|
276
|
+
id: data.id,
|
|
277
|
+
firstName: data.firstName,
|
|
278
|
+
lastName: data.lastName,
|
|
279
|
+
displayName: data.displayName,
|
|
280
|
+
phone: data.phone,
|
|
281
|
+
accountNo: data.accountNo,
|
|
282
|
+
email: data.email,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
catch (error) {
|
|
286
|
+
console.error("Error creating profile from object:", error);
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Type guard to check if an unknown object is a valid Profile instance.
|
|
292
|
+
*
|
|
293
|
+
* @param obj - The object to check
|
|
294
|
+
* @returns Type predicate indicating if the object is a valid Profile
|
|
295
|
+
*/
|
|
296
|
+
static is(obj) {
|
|
297
|
+
if (!obj || typeof obj !== "object")
|
|
298
|
+
return false;
|
|
299
|
+
const maybeProfile = obj;
|
|
300
|
+
// Check required properties
|
|
301
|
+
if (typeof maybeProfile._id !== "string" ||
|
|
302
|
+
typeof maybeProfile._displayName !== "string" ||
|
|
303
|
+
typeof maybeProfile._accountNo !== "string") {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
// Check nullable/optional properties have the right type when present
|
|
307
|
+
if (maybeProfile._firstName !== null &&
|
|
308
|
+
maybeProfile._firstName !== undefined &&
|
|
309
|
+
typeof maybeProfile._firstName !== "string") {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
if (maybeProfile._lastName !== null &&
|
|
313
|
+
maybeProfile._lastName !== undefined &&
|
|
314
|
+
typeof maybeProfile._lastName !== "string") {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
if (maybeProfile._email !== null &&
|
|
318
|
+
maybeProfile._email !== undefined &&
|
|
319
|
+
typeof maybeProfile._email !== "string") {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
// Check phone number
|
|
323
|
+
const phone = maybeProfile._phone;
|
|
324
|
+
if (phone !== null &&
|
|
325
|
+
phone !== undefined &&
|
|
326
|
+
!PhoneNumber.is(phone)) {
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Profile } from "./profile.js";
|
|
2
2
|
/**
|
|
3
|
-
* Represents a user in Afloat
|
|
3
|
+
* Represents a user in Afloat.
|
|
4
4
|
*
|
|
5
5
|
* This class centralizes user-related logic, simplifying interaction
|
|
6
6
|
* with user-related data and ensuring consistent permission checks across the application.
|
|
@@ -38,19 +38,10 @@ export declare class User {
|
|
|
38
38
|
/**
|
|
39
39
|
* Creates a new instance of the `User` class.
|
|
40
40
|
*
|
|
41
|
-
* @param userData - An object
|
|
41
|
+
* @param userData - An object containing the user's profile, token,
|
|
42
42
|
* permissions (access list), and the `resetPassword` flag.
|
|
43
43
|
*/
|
|
44
|
-
constructor(
|
|
45
|
-
profile: Profile;
|
|
46
|
-
token: string;
|
|
47
|
-
access: string[];
|
|
48
|
-
resetPassword: boolean;
|
|
49
|
-
loginCredentials: {
|
|
50
|
-
name: string;
|
|
51
|
-
identity: string;
|
|
52
|
-
};
|
|
53
|
-
});
|
|
44
|
+
private constructor();
|
|
54
45
|
/**
|
|
55
46
|
* Checks if the user has a specific permission.
|
|
56
47
|
*
|
|
@@ -62,7 +53,7 @@ export declare class User {
|
|
|
62
53
|
* Serializes the `User` instance to a JSON string.
|
|
63
54
|
*
|
|
64
55
|
* @returns A JSON string representation of the `User` instance, including:
|
|
65
|
-
* - `profile`: The user's profile information.
|
|
56
|
+
* - `profile`: The user's profile information. (Requires profile.toJSON() method)
|
|
66
57
|
* - `token`: The user's authentication token.
|
|
67
58
|
* - `resetPassword`: Indicates whether the user must reset their password.
|
|
68
59
|
* - `permissions`: An array of permission keys the user has.
|
|
@@ -72,9 +63,15 @@ export declare class User {
|
|
|
72
63
|
* Creates a new `User` instance from a JSON string.
|
|
73
64
|
*
|
|
74
65
|
* @param jsonString - A JSON string containing user data.
|
|
75
|
-
* @returns A `User` instance reconstructed from the JSON data.
|
|
76
|
-
* @throws Will throw an error if the JSON data is invalid or incomplete.
|
|
66
|
+
* @returns A `User` instance reconstructed from the JSON data, or undefined if jsonString is invalid.
|
|
77
67
|
*/
|
|
78
|
-
static fromJSON(jsonString: string): User;
|
|
68
|
+
static fromJSON(jsonString: string): User | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new `User` instance from a data object, JSON string, or object with a Profile instance/object.
|
|
71
|
+
*
|
|
72
|
+
* @param data - The data object, JSON string, or object with Profile instance/object containing user information.
|
|
73
|
+
* @returns A `User` instance, or undefined if data is invalid.
|
|
74
|
+
*/
|
|
75
|
+
static from(data: any): User | undefined;
|
|
79
76
|
}
|
|
80
77
|
//# sourceMappingURL=user.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../../../src/src/models/user/user.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../../../src/src/models/user/user.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC;;;;;GAKG;AACH,qBAAa,IAAI;IACf;;OAEG;IACI,IAAI,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACI,QAAQ,EAAE,MAAM,CAAC;IAExB;;OAEG;IACI,OAAO,EAAE,OAAO,CAAC;IAExB;;;OAGG;IACI,KAAK,EAAE,MAAM,CAAC;IAErB;;;;;OAKG;IACI,aAAa,EAAE,OAAO,CAAC;IAE9B;;;OAGG;IACH,OAAO,CAAC,cAAc,CAA0B;IAEhD;;;;;OAKG;IACH,OAAO;IA6BP;;;;;OAKG;IACI,GAAG,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO;IAIvC;;;;;;;;OAQG;IACI,MAAM,IAAI,MAAM;IAavB;;;;;OAKG;WACW,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAS5D;;;;;OAKG;WACW,IAAI,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,GAAG,SAAS;CA4FhD"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
// deno-lint-ignore-file no-explicit-any
|
|
1
2
|
import { Permissions } from "../permission.js";
|
|
3
|
+
import { Profile } from "./profile.js";
|
|
2
4
|
/**
|
|
3
|
-
* Represents a user in Afloat
|
|
5
|
+
* Represents a user in Afloat.
|
|
4
6
|
*
|
|
5
7
|
* This class centralizes user-related logic, simplifying interaction
|
|
6
8
|
* with user-related data and ensuring consistent permission checks across the application.
|
|
@@ -9,7 +11,7 @@ export class User {
|
|
|
9
11
|
/**
|
|
10
12
|
* Creates a new instance of the `User` class.
|
|
11
13
|
*
|
|
12
|
-
* @param userData - An object
|
|
14
|
+
* @param userData - An object containing the user's profile, token,
|
|
13
15
|
* permissions (access list), and the `resetPassword` flag.
|
|
14
16
|
*/
|
|
15
17
|
constructor(data) {
|
|
@@ -72,12 +74,12 @@ export class User {
|
|
|
72
74
|
writable: true,
|
|
73
75
|
value: void 0
|
|
74
76
|
});
|
|
75
|
-
const { profile, token, access, resetPassword } = data;
|
|
77
|
+
const { profile, token, access, resetPassword, name, identity } = data;
|
|
76
78
|
this.profile = profile;
|
|
77
79
|
this.token = token;
|
|
78
80
|
this.resetPassword = resetPassword;
|
|
79
|
-
this.name =
|
|
80
|
-
this.identity =
|
|
81
|
+
this.name = name;
|
|
82
|
+
this.identity = identity;
|
|
81
83
|
// Initialize the permissions map
|
|
82
84
|
this.permissionsMap = {};
|
|
83
85
|
for (const permission of Object.values(Permissions)) {
|
|
@@ -86,6 +88,9 @@ export class User {
|
|
|
86
88
|
this.permissionsMap[perm] = access.includes(perm);
|
|
87
89
|
});
|
|
88
90
|
}
|
|
91
|
+
else {
|
|
92
|
+
this.permissionsMap[permission] = access.includes(permission);
|
|
93
|
+
}
|
|
89
94
|
}
|
|
90
95
|
}
|
|
91
96
|
/**
|
|
@@ -101,14 +106,14 @@ export class User {
|
|
|
101
106
|
* Serializes the `User` instance to a JSON string.
|
|
102
107
|
*
|
|
103
108
|
* @returns A JSON string representation of the `User` instance, including:
|
|
104
|
-
* - `profile`: The user's profile information.
|
|
109
|
+
* - `profile`: The user's profile information. (Requires profile.toJSON() method)
|
|
105
110
|
* - `token`: The user's authentication token.
|
|
106
111
|
* - `resetPassword`: Indicates whether the user must reset their password.
|
|
107
112
|
* - `permissions`: An array of permission keys the user has.
|
|
108
113
|
*/
|
|
109
114
|
toJSON() {
|
|
110
115
|
return JSON.stringify({
|
|
111
|
-
profile: this.profile,
|
|
116
|
+
profile: this.profile.toObject(),
|
|
112
117
|
token: this.token,
|
|
113
118
|
resetPassword: this.resetPassword,
|
|
114
119
|
name: this.name,
|
|
@@ -120,29 +125,107 @@ export class User {
|
|
|
120
125
|
* Creates a new `User` instance from a JSON string.
|
|
121
126
|
*
|
|
122
127
|
* @param jsonString - A JSON string containing user data.
|
|
123
|
-
* @returns A `User` instance reconstructed from the JSON data.
|
|
124
|
-
* @throws Will throw an error if the JSON data is invalid or incomplete.
|
|
128
|
+
* @returns A `User` instance reconstructed from the JSON data, or undefined if jsonString is invalid.
|
|
125
129
|
*/
|
|
126
130
|
static fromJSON(jsonString) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
131
|
+
try {
|
|
132
|
+
return User.from(JSON.parse(jsonString));
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
console.error("Invalid JSON string:", e);
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Creates a new `User` instance from a data object, JSON string, or object with a Profile instance/object.
|
|
141
|
+
*
|
|
142
|
+
* @param data - The data object, JSON string, or object with Profile instance/object containing user information.
|
|
143
|
+
* @returns A `User` instance, or undefined if data is invalid.
|
|
144
|
+
*/
|
|
145
|
+
static from(data) {
|
|
146
|
+
let parsedData;
|
|
147
|
+
// Parse JSON string if needed
|
|
148
|
+
if (typeof data === "string") {
|
|
149
|
+
try {
|
|
150
|
+
parsedData = JSON.parse(data);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
console.error("Invalid JSON string:", error);
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
parsedData = data;
|
|
159
|
+
}
|
|
160
|
+
if (!parsedData) {
|
|
161
|
+
console.error("Data is null or undefined.");
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
// Handle different profile formats
|
|
165
|
+
let profile;
|
|
166
|
+
let rawProfile = parsedData.profile;
|
|
167
|
+
// Handle stringified profile (the case in the provided sample)
|
|
168
|
+
if (typeof rawProfile === "string") {
|
|
169
|
+
try {
|
|
170
|
+
// Attempt to parse the stringified profile
|
|
171
|
+
rawProfile = JSON.parse(rawProfile);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
console.error("Failed to parse profile JSON string:", error);
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Create Profile instance based on what we received
|
|
179
|
+
if (Profile.is(rawProfile)) {
|
|
180
|
+
// Already a Profile instance
|
|
181
|
+
profile = rawProfile;
|
|
182
|
+
}
|
|
183
|
+
else if (typeof rawProfile === "object" && rawProfile !== null) {
|
|
184
|
+
// Convert object to Profile instance
|
|
185
|
+
profile = Profile.from(rawProfile);
|
|
186
|
+
if (!profile) {
|
|
187
|
+
console.error("Failed to create Profile from data:", rawProfile);
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else if (typeof rawProfile === "string") {
|
|
192
|
+
profile = Profile.fromJSON(rawProfile);
|
|
193
|
+
if (!profile) {
|
|
194
|
+
console.error("Failed to create Profile from JSON data:", rawProfile);
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
console.error("Invalid profile format:", typeof rawProfile);
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
// Validate other required fields
|
|
203
|
+
if (!parsedData.token || typeof parsedData.token !== "string" ||
|
|
204
|
+
!parsedData.name || typeof parsedData.name !== "string" ||
|
|
205
|
+
!parsedData.identity || typeof parsedData.identity !== "string" ||
|
|
206
|
+
!Array.isArray(parsedData.permissions) &&
|
|
207
|
+
!Array.isArray(parsedData.access) ||
|
|
208
|
+
typeof parsedData.resetPassword !== "boolean") {
|
|
209
|
+
console.error("Missing or invalid required User fields:", {
|
|
210
|
+
token: typeof parsedData.token,
|
|
211
|
+
name: typeof parsedData.name,
|
|
212
|
+
identity: typeof parsedData.identity,
|
|
213
|
+
permissions: Array.isArray(parsedData.permissions),
|
|
214
|
+
access: Array.isArray(parsedData.access),
|
|
215
|
+
resetPassword: typeof parsedData.resetPassword,
|
|
216
|
+
});
|
|
217
|
+
return undefined;
|
|
135
218
|
}
|
|
136
|
-
//
|
|
219
|
+
// Support both 'permissions' and 'access' field names
|
|
220
|
+
const access = parsedData.access || parsedData.permissions;
|
|
221
|
+
// Create and return the User instance
|
|
137
222
|
const args = {
|
|
138
|
-
profile:
|
|
139
|
-
token:
|
|
140
|
-
access:
|
|
141
|
-
resetPassword:
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
identity: data.identity,
|
|
145
|
-
},
|
|
223
|
+
profile: profile,
|
|
224
|
+
token: parsedData.token,
|
|
225
|
+
access: access,
|
|
226
|
+
resetPassword: parsedData.resetPassword,
|
|
227
|
+
name: parsedData.name,
|
|
228
|
+
identity: parsedData.identity,
|
|
146
229
|
};
|
|
147
230
|
return new User(args);
|
|
148
231
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/models/wallet/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/models/wallet/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
|