@wawjs/ngx-bos 22.0.4 → 22.0.6
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/AGENTS.md +4 -0
- package/README.md +12 -5
- package/RELEASES.md +15 -0
- package/ai/code-style.md +15 -0
- package/fesm2022/wawjs-ngx-bos-ngx-bos.config-B6tWU_X3.mjs +29 -0
- package/fesm2022/wawjs-ngx-bos-ngx-bos.config-B6tWU_X3.mjs.map +1 -0
- package/fesm2022/wawjs-ngx-bos-user.service-DFgHcvNR.mjs +187 -0
- package/fesm2022/wawjs-ngx-bos-user.service-DFgHcvNR.mjs.map +1 -0
- package/fesm2022/wawjs-ngx-bos.mjs +3065 -2293
- package/fesm2022/wawjs-ngx-bos.mjs.map +1 -1
- package/package.json +10 -10
- package/src/feature/file/file.component.ts +56 -53
- package/src/feature/file/file.service.ts +4 -3
- package/src/feature/file/file.util.ts +9 -2
- package/src/feature/file/pages/files/files.component.html +1 -1
- package/src/feature/file/pages/files/files.component.ts +5 -6
- package/src/feature/user/guards/admins.guard.ts +5 -12
- package/src/feature/user/guards/authenticated.guard.ts +3 -10
- package/src/feature/user/guards/guest.guard.ts +3 -3
- package/src/feature/user/pages/users/users.component.ts +2 -6
- package/src/feature/user/user.service.ts +25 -28
- package/src/feature/user/user.storage.ts +31 -0
- package/src/icons/spider/spider.component.ts +4 -4
- package/src/ngx-bos.config.ts +1 -1
- package/src/pages/form/form.component.ts +21 -7
- package/src/pages/forms/forms.component.ts +9 -22
- package/src/public-api.ts +1 -1
- package/types/wawjs-ngx-bos.d.ts +402 -305
package/AGENTS.md
CHANGED
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ export const appConfig = {
|
|
|
51
51
|
userForm: environment.userForm ?? [],
|
|
52
52
|
signPath: '/sign',
|
|
53
53
|
profilePath: '/profile',
|
|
54
|
-
defaultUserThumb: 'default.png',
|
|
54
|
+
defaultUserThumb: 'default.png',
|
|
55
55
|
}),
|
|
56
56
|
// ...other WAW providers
|
|
57
57
|
],
|
|
@@ -68,7 +68,7 @@ export const appConfig = {
|
|
|
68
68
|
| `userForm` | `[]` | Form component descriptors for the user form |
|
|
69
69
|
| `signPath` | `'/sign'` | Route to redirect to on logout |
|
|
70
70
|
| `profilePath` | `'/profile'` | Route for the signed-in user's profile / avatar link |
|
|
71
|
-
| `defaultUserThumb` | `'default.png'` | Fallback avatar when the user has no thumbnail |
|
|
71
|
+
| `defaultUserThumb` | `'default.png'` | Fallback avatar when the user has no thumbnail |
|
|
72
72
|
|
|
73
73
|
The resolved config is available through the `NGX_BOS_CONFIG` injection token.
|
|
74
74
|
|
|
@@ -76,7 +76,7 @@ The resolved config is available through the `NGX_BOS_CONFIG` injection token.
|
|
|
76
76
|
|
|
77
77
|
- **Config:** `ngxBosProvide` / `provideNgxBos`, `NGX_BOS_CONFIG`, `DEFAULT_NGX_BOS_CONFIG`, `NgxBosConfig`, `NGX_BOS_VERSION`
|
|
78
78
|
- **User / auth:** `UserService`, `User` interface, user const/form, `UserSelectorComponent`, `UserPreviewComponent`, `UsersComponent`, `ClientsComponent`
|
|
79
|
-
- **Guards:** `
|
|
79
|
+
- **Guards:** `authenticatedGuard`, `guestGuard`, `adminsGuard` (functional `CanActivateFn`)
|
|
80
80
|
- **File:** `FileSelectorComponent`, `FilesComponent`, file feature contracts
|
|
81
81
|
- **Form pages:** `FormComponent`, `FormsComponent`
|
|
82
82
|
- **Routes:** `usersRoutes`, `clientsRoutes`, `filesRoutes`, `formRoutes`, `formsRoutes`
|
|
@@ -85,13 +85,20 @@ The resolved config is available through the `NGX_BOS_CONFIG` injection token.
|
|
|
85
85
|
### Using package routes
|
|
86
86
|
|
|
87
87
|
```ts
|
|
88
|
-
import { usersRoutes,
|
|
88
|
+
import { usersRoutes, adminsGuard } from '@wawjs/ngx-bos';
|
|
89
89
|
|
|
90
90
|
export const routes: Routes = [
|
|
91
|
-
{ path: 'users', canActivate: [
|
|
91
|
+
{ path: 'users', canActivate: [adminsGuard], children: usersRoutes },
|
|
92
92
|
];
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
+
Prefer the `*Routes` exports over importing the page components directly. The page components
|
|
96
|
+
(`UsersComponent`, `ClientsComponent`, `FilesComponent`, `FormComponent`, `FormsComponent`) are
|
|
97
|
+
still exported for backward compatibility, so the package is a single entry point and their
|
|
98
|
+
`loadComponent()` calls do not produce separate chunks: `ng build ngx-bos` reports
|
|
99
|
+
`INEFFECTIVE_DYNAMIC_IMPORT` for them. This is expected; the page components ship with the package
|
|
100
|
+
bundle either way.
|
|
101
|
+
|
|
95
102
|
## Building
|
|
96
103
|
|
|
97
104
|
From the workspace root:
|
package/RELEASES.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Releases
|
|
2
2
|
|
|
3
|
+
## 22.0.5
|
|
4
|
+
|
|
5
|
+
Bug-fix release on the `@wawjs/*` 22.0.12 line.
|
|
6
|
+
|
|
7
|
+
- `FilesComponent` shows loaded files again (rows are a signal) and registers its `cell` templates.
|
|
8
|
+
- `FileService.uploadBase64` rejects on HTTP errors; the file picker keeps the files that did upload and always resets.
|
|
9
|
+
- The form page reads `formId` from the route, so moving between forms loads the right one; the delete confirm names the form component.
|
|
10
|
+
- `UserService` and the user guards no longer crash on a corrupt saved user: the bad value is removed.
|
|
11
|
+
- Removed leftover SSR branches from the guards and `UserService` (BOS is browser-only).
|
|
12
|
+
|
|
13
|
+
### Migration notes
|
|
14
|
+
|
|
15
|
+
- Update all `@wawjs/*` dependencies together to `22.0.12`.
|
|
16
|
+
- Optional: the template app now uses the `@wawjs/ngx-ui` startup screen (`provideStartup()`, `startup.css`, markup in `index.html`) and no longer depends on `zone.js`.
|
|
17
|
+
|
|
3
18
|
## 22.0.4
|
|
4
19
|
|
|
5
20
|
Companion package update.
|
package/ai/code-style.md
CHANGED
|
@@ -31,3 +31,18 @@ Follow Angular 22 and local BOS conventions.
|
|
|
31
31
|
|
|
32
32
|
Use the signal-based forms API for new forms when feasible. Keep dynamic form schemas registered
|
|
33
33
|
through the existing form component registry.
|
|
34
|
+
|
|
35
|
+
## Translation Files
|
|
36
|
+
|
|
37
|
+
- Translations use `@wawjs/ngx-translate` compact string arrays. `en.json` is the ordered
|
|
38
|
+
source array; every other language file in the same folder keeps the same order and length.
|
|
39
|
+
- `src/i18n/<lang>.json` holds shared interface text: layouts, shared components, common messages,
|
|
40
|
+
and pages with little text.
|
|
41
|
+
- A page with a large amount of text (for example a long landing page) gets its own bundle in
|
|
42
|
+
`src/i18n/<page>/<lang>.json`, with its own `en.json` source array. Load it from the page
|
|
43
|
+
with `TranslateService.loadExtraTranslation('/i18n/<page>/')`. Do not create page folders for
|
|
44
|
+
pages with only a few strings.
|
|
45
|
+
- Write attributes such as `alt` and `aria-label` as plain source-language text so they are present
|
|
46
|
+
in the rendered HTML.
|
|
47
|
+
- This is a browser-only SPA: source text is what users see until the translation file loads.
|
|
48
|
+
- When a string is removed from the source array, remove the same index from every language file.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { InjectionToken, makeEnvironmentProviders } from "@angular/core";
|
|
2
|
+
const DEFAULT_NGX_BOS_CONFIG = {
|
|
3
|
+
appId: "",
|
|
4
|
+
url: "",
|
|
5
|
+
cdnUrl: "https://cdn.webart.work",
|
|
6
|
+
roles: [],
|
|
7
|
+
userFields: [],
|
|
8
|
+
userForm: [],
|
|
9
|
+
signPath: "/sign",
|
|
10
|
+
profilePath: "/profile",
|
|
11
|
+
defaultUserThumb: "default.png"
|
|
12
|
+
};
|
|
13
|
+
const NGX_BOS_CONFIG = new InjectionToken("NGX_BOS_CONFIG", { factory: () => DEFAULT_NGX_BOS_CONFIG });
|
|
14
|
+
function ngxBosProvide(config = {}) {
|
|
15
|
+
return makeEnvironmentProviders([{
|
|
16
|
+
provide: NGX_BOS_CONFIG,
|
|
17
|
+
useValue: {
|
|
18
|
+
...DEFAULT_NGX_BOS_CONFIG,
|
|
19
|
+
...config,
|
|
20
|
+
roles: config.roles ?? DEFAULT_NGX_BOS_CONFIG.roles,
|
|
21
|
+
userFields: config.userFields ?? DEFAULT_NGX_BOS_CONFIG.userFields,
|
|
22
|
+
userForm: config.userForm ?? DEFAULT_NGX_BOS_CONFIG.userForm
|
|
23
|
+
}
|
|
24
|
+
}]);
|
|
25
|
+
}
|
|
26
|
+
const provideNgxBos = ngxBosProvide;
|
|
27
|
+
export { provideNgxBos as i, NGX_BOS_CONFIG as n, ngxBosProvide as r, DEFAULT_NGX_BOS_CONFIG as t };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=wawjs-ngx-bos-ngx-bos.config-B6tWU_X3.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wawjs-ngx-bos-ngx-bos.config-B6tWU_X3.mjs","names":[],"sources":["../../../projects/ngx-bos/src/ngx-bos.config.ts"],"sourcesContent":["import {\r\n\tEnvironmentProviders,\r\n\tInjectionToken,\r\n\tProvider,\r\n\tmakeEnvironmentProviders,\r\n} from '@angular/core';\r\nimport { FormComponentInterface } from '@wawjs/ngx-form';\r\n\r\nexport interface NgxBosConfig {\r\n\tappId?: string;\r\n\turl?: string;\r\n\tcdnUrl?: string;\r\n\troles?: string[];\r\n\tuserFields?: string[];\r\n\tuserForm?: FormComponentInterface[];\r\n\tsignPath?: string;\r\n\tprofilePath?: string;\r\n\tdefaultUserThumb?: string;\r\n}\r\n\r\nexport const DEFAULT_NGX_BOS_CONFIG: Required<NgxBosConfig> = {\r\n\tappId: '',\r\n\turl: '',\r\n\tcdnUrl: 'https://cdn.webart.work',\r\n\troles: [],\r\n\tuserFields: [],\r\n\tuserForm: [],\r\n\tsignPath: '/sign',\r\n\tprofilePath: '/profile',\r\n\tdefaultUserThumb: 'default.png',\r\n};\r\n\r\nexport const NGX_BOS_CONFIG = new InjectionToken<Required<NgxBosConfig>>(\r\n\t'NGX_BOS_CONFIG',\r\n\t{\r\n\t\tfactory: () => DEFAULT_NGX_BOS_CONFIG,\r\n\t},\r\n);\r\n\r\nexport function ngxBosProvide(config: NgxBosConfig = {}): EnvironmentProviders {\r\n\treturn makeEnvironmentProviders([\r\n\t\t{\r\n\t\t\tprovide: NGX_BOS_CONFIG,\r\n\t\t\tuseValue: {\r\n\t\t\t\t...DEFAULT_NGX_BOS_CONFIG,\r\n\t\t\t\t...config,\r\n\t\t\t\troles: config.roles ?? DEFAULT_NGX_BOS_CONFIG.roles,\r\n\t\t\t\tuserFields: config.userFields ?? DEFAULT_NGX_BOS_CONFIG.userFields,\r\n\t\t\t\tuserForm: config.userForm ?? DEFAULT_NGX_BOS_CONFIG.userForm,\r\n\t\t\t},\r\n\t\t} satisfies Provider,\r\n\t]);\r\n}\r\n\r\nexport const provideNgxBos = ngxBosProvide;\r\n"],"mappings":";AAoBA,MAAa,yBAAiD;CAC7D,OAAO;CACP,KAAK;CACL,QAAQ;CACR,OAAO,CAAA;CACP,YAAY,CAAA;CACZ,UAAU,CAAA;CACV,UAAU;CACV,aAAa;CACb,kBAAkB;AAClB;AAED,MAAa,iBAAiB,IAAI,eACjC,kBACA,EACC,eAAe,uBACf,CAAA;AAGF,SAAgB,cAAc,SAAuB,CAAA,GAAE;CACtD,OAAO,yBAAyB,CAC/B;EACC,SAAS;EACT,UAAU;GACT,GAAG;GACH,GAAG;GACH,OAAO,OAAO,SAAS,uBAAuB;GAC9C,YAAY,OAAO,cAAc,uBAAuB;GACxD,UAAU,OAAO,YAAY,uBAAuB;EACpD;CACkB,CACpB,CAAA;AACF;AAEA,MAAa,gBAAgB"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { n as NGX_BOS_CONFIG } from "./wawjs-ngx-bos-ngx-bos.config-B6tWU_X3.mjs";
|
|
2
|
+
import * as i0 from "@angular/core";
|
|
3
|
+
import { Service, computed, inject, signal } from "@angular/core";
|
|
4
|
+
import { AlertService } from "@wawjs/ngx-ui";
|
|
5
|
+
import { CrudService } from "@wawjs/ngx-crud";
|
|
6
|
+
import { HttpService } from "@wawjs/ngx-http";
|
|
7
|
+
import { EmitterService } from "@wawjs/ngx-core";
|
|
8
|
+
import { Router } from "@angular/router";
|
|
9
|
+
const createUserFormComponents = (userForm = []) => ({
|
|
10
|
+
formId: "user",
|
|
11
|
+
title: "Profile Settings",
|
|
12
|
+
components: [
|
|
13
|
+
{
|
|
14
|
+
name: "Input",
|
|
15
|
+
key: "name",
|
|
16
|
+
focused: true,
|
|
17
|
+
props: {
|
|
18
|
+
placeholder: "Enter name...",
|
|
19
|
+
label: "Name"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "Input",
|
|
24
|
+
key: "email",
|
|
25
|
+
props: {
|
|
26
|
+
placeholder: "Enter email...",
|
|
27
|
+
label: "Email",
|
|
28
|
+
type: "email"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "Input",
|
|
33
|
+
key: "phone",
|
|
34
|
+
props: {
|
|
35
|
+
placeholder: "Enter phone...",
|
|
36
|
+
label: "Phone"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: "Input",
|
|
41
|
+
key: "bio",
|
|
42
|
+
props: {
|
|
43
|
+
placeholder: "Enter bio...",
|
|
44
|
+
label: "Biography",
|
|
45
|
+
type: "textarea"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
...userForm
|
|
49
|
+
]
|
|
50
|
+
});
|
|
51
|
+
const userFormComponentsFactory = () => {
|
|
52
|
+
const config = inject(NGX_BOS_CONFIG);
|
|
53
|
+
return createUserFormComponents(config.userForm);
|
|
54
|
+
};
|
|
55
|
+
const USER_STORAGE_KEY = "waw_user";
|
|
56
|
+
function readStoredUser() {
|
|
57
|
+
const stored = localStorage.getItem(USER_STORAGE_KEY);
|
|
58
|
+
if (!stored) return null;
|
|
59
|
+
try {
|
|
60
|
+
const user = JSON.parse(stored);
|
|
61
|
+
if (user && typeof user === "object") return user;
|
|
62
|
+
} catch {}
|
|
63
|
+
localStorage.removeItem(USER_STORAGE_KEY);
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
var UserService = class UserService extends CrudService {
|
|
67
|
+
role(role) {
|
|
68
|
+
return !!(this.user().is || {})[role];
|
|
69
|
+
}
|
|
70
|
+
constructor() {
|
|
71
|
+
super({
|
|
72
|
+
name: "user",
|
|
73
|
+
replace: (user) => {
|
|
74
|
+
user.roles = [];
|
|
75
|
+
user.data = user.data || {};
|
|
76
|
+
user.is = user.is || {};
|
|
77
|
+
for (const field of this._bosConfig.userFields) user.data[field] = user.data[field] || {};
|
|
78
|
+
for (const role of this.roles) if (user.is[role]) user.roles.push(role);
|
|
79
|
+
return user;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
this._userHttpService = inject(HttpService);
|
|
83
|
+
this._alertService = inject(AlertService);
|
|
84
|
+
this._router = inject(Router);
|
|
85
|
+
this._emitterService = inject(EmitterService);
|
|
86
|
+
this._bosConfig = inject(NGX_BOS_CONFIG);
|
|
87
|
+
this.url = this._bosConfig.url;
|
|
88
|
+
this.roles = this._bosConfig.roles.concat(["admin"]);
|
|
89
|
+
this.employees = this._bosConfig.roles;
|
|
90
|
+
this.users = this.documents;
|
|
91
|
+
this.user = signal(readStoredUser() ?? this.prepareDocument()(), ...ngDevMode ? [{ debugName: "user" }] : /* istanbul ignore next */ []);
|
|
92
|
+
this.thumb = computed(() => {
|
|
93
|
+
const thumb = this.user().thumb;
|
|
94
|
+
return !thumb || thumb.includes(this._bosConfig.defaultUserThumb) ? this._bosConfig.defaultUserThumb : /^https?:\/\//i.test(thumb) ? thumb : this.url + thumb;
|
|
95
|
+
}, ...ngDevMode ? [{ debugName: "thumb" }] : /* istanbul ignore next */ []);
|
|
96
|
+
this.usersByRole = computed(() => {
|
|
97
|
+
const splitted = {};
|
|
98
|
+
for (const user of this.documents()) for (const role of user.roles || []) {
|
|
99
|
+
if (role === void 0 || role === null) continue;
|
|
100
|
+
(splitted[String(role)] ??= []).push(user);
|
|
101
|
+
}
|
|
102
|
+
return splitted;
|
|
103
|
+
}, ...ngDevMode ? [{ debugName: "usersByRole" }] : /* istanbul ignore next */ []);
|
|
104
|
+
if (localStorage.getItem("waw_user")) {
|
|
105
|
+
this.fetch({}, { name: "me" }).subscribe((user) => {
|
|
106
|
+
if (user) this.setUser(user);
|
|
107
|
+
else if (localStorage.getItem("waw_user")) this.logout();
|
|
108
|
+
});
|
|
109
|
+
this.get({ query: this._bosConfig.appId ? "appId=" + this._bosConfig.appId : "" });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
setUser(user) {
|
|
113
|
+
this.user.set(user);
|
|
114
|
+
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(user));
|
|
115
|
+
this._emitterService.complete("us.user");
|
|
116
|
+
}
|
|
117
|
+
updateMe() {
|
|
118
|
+
this.setUser(this.user());
|
|
119
|
+
return this.update(this.user()).subscribe(() => {
|
|
120
|
+
this._alertService.info({ text: "Profile information has been updated" });
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
updateMeAfterWhile() {
|
|
124
|
+
this.setUser(this.user());
|
|
125
|
+
this.updateAfterWhile(this.user());
|
|
126
|
+
}
|
|
127
|
+
changePassword(oldPass, newPass) {
|
|
128
|
+
return this._userHttpService.post("/api/user/changePassword", {
|
|
129
|
+
newPass,
|
|
130
|
+
oldPass
|
|
131
|
+
}, (resp) => {
|
|
132
|
+
if (resp) this._alertService.info({ text: "Successfully changed password" });
|
|
133
|
+
else this._alertService.error({ text: "Incorrect current password" });
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
logout() {
|
|
137
|
+
this.user.set(this.prepareDocument()());
|
|
138
|
+
localStorage.removeItem(USER_STORAGE_KEY);
|
|
139
|
+
let finished = false;
|
|
140
|
+
const finish = () => {
|
|
141
|
+
if (finished) return;
|
|
142
|
+
finished = true;
|
|
143
|
+
this._router.navigateByUrl(this._bosConfig.signPath);
|
|
144
|
+
setTimeout(() => {
|
|
145
|
+
location.reload();
|
|
146
|
+
}, 100);
|
|
147
|
+
};
|
|
148
|
+
this._userHttpService.get("/api/user/logout", finish, { err: finish });
|
|
149
|
+
this._userHttpService.remove("token");
|
|
150
|
+
setTimeout(finish, 3e3);
|
|
151
|
+
}
|
|
152
|
+
updateAdmin(user) {
|
|
153
|
+
return this.update(user, { name: "admin" });
|
|
154
|
+
}
|
|
155
|
+
deleteAdmin(user) {
|
|
156
|
+
return this.delete(user, { name: "admin" });
|
|
157
|
+
}
|
|
158
|
+
static {
|
|
159
|
+
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
160
|
+
minVersion: "12.0.0",
|
|
161
|
+
version: "22.2.5",
|
|
162
|
+
ngImport: i0,
|
|
163
|
+
type: UserService,
|
|
164
|
+
deps: [],
|
|
165
|
+
target: i0.ɵɵFactoryTarget.Service
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
static {
|
|
169
|
+
this.ɵprov = i0.ɵɵngDeclareService({
|
|
170
|
+
minVersion: "22.0.0",
|
|
171
|
+
version: "22.2.5",
|
|
172
|
+
ngImport: i0,
|
|
173
|
+
type: UserService
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
178
|
+
minVersion: "12.0.0",
|
|
179
|
+
version: "22.2.5",
|
|
180
|
+
ngImport: i0,
|
|
181
|
+
type: UserService,
|
|
182
|
+
decorators: [{ type: Service }],
|
|
183
|
+
ctorParameters: () => []
|
|
184
|
+
});
|
|
185
|
+
export { userFormComponentsFactory as i, readStoredUser as n, createUserFormComponents as r, UserService as t };
|
|
186
|
+
|
|
187
|
+
//# sourceMappingURL=wawjs-ngx-bos-user.service-DFgHcvNR.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wawjs-ngx-bos-user.service-DFgHcvNR.mjs","names":[],"sources":["../../../projects/ngx-bos/src/feature/user/user.form.ts","../../../projects/ngx-bos/src/feature/user/user.storage.ts","../../../projects/ngx-bos/src/feature/user/user.service.ts"],"sourcesContent":["import { inject } from '@angular/core';\r\nimport { FormComponentInterface } from '@wawjs/ngx-form';\r\nimport { NGX_BOS_CONFIG } from '../../ngx-bos.config';\r\n\r\nexport const createUserFormComponents = (\r\n\tuserForm: FormComponentInterface[] = [],\r\n) => ({\r\n\tformId: 'user',\r\n\ttitle: 'Profile Settings',\r\n\tcomponents: [\r\n\t\t{\r\n\t\t\tname: 'Input',\r\n\t\t\tkey: 'name',\r\n\t\t\tfocused: true,\r\n\t\t\tprops: {\r\n\t\t\t\tplaceholder: 'Enter name...',\r\n\t\t\t\tlabel: 'Name',\r\n\t\t\t},\r\n\t\t},\r\n\t\t{\r\n\t\t\tname: 'Input',\r\n\t\t\tkey: 'email',\r\n\t\t\tprops: {\r\n\t\t\t\tplaceholder: 'Enter email...',\r\n\t\t\t\tlabel: 'Email',\r\n\t\t\t\ttype: 'email',\r\n\t\t\t},\r\n\t\t},\r\n\t\t{\r\n\t\t\tname: 'Input',\r\n\t\t\tkey: 'phone',\r\n\t\t\tprops: {\r\n\t\t\t\tplaceholder: 'Enter phone...',\r\n\t\t\t\tlabel: 'Phone',\r\n\t\t\t},\r\n\t\t},\r\n\t\t{\r\n\t\t\tname: 'Input',\r\n\t\t\tkey: 'bio',\r\n\t\t\tprops: {\r\n\t\t\t\tplaceholder: 'Enter bio...',\r\n\t\t\t\tlabel: 'Biography',\r\n\t\t\t\ttype: 'textarea',\r\n\t\t\t},\r\n\t\t},\r\n\t\t...userForm,\r\n\t],\r\n});\r\n\r\nexport const userFormComponentsFactory = () => {\r\n\tconst config = inject(NGX_BOS_CONFIG);\r\n\r\n\treturn createUserFormComponents(config.userForm);\r\n};\r\n","import { User } from './user.interface';\r\n\r\n/** localStorage key holding the signed-in user snapshot. */\r\nexport const USER_STORAGE_KEY = 'waw_user';\r\n\r\n/**\r\n * Reads the stored user snapshot. Returns `null` when nothing is stored or when the\r\n * stored value is corrupted; a corrupted value is removed so it cannot break guards\r\n * or service initialization again.\r\n */\r\nexport function readStoredUser(): User | null {\r\n\tconst stored = localStorage.getItem(USER_STORAGE_KEY);\r\n\r\n\tif (!stored) {\r\n\t\treturn null;\r\n\t}\r\n\r\n\ttry {\r\n\t\tconst user = JSON.parse(stored) as User | null;\r\n\r\n\t\tif (user && typeof user === 'object') {\r\n\t\t\treturn user;\r\n\t\t}\r\n\t} catch {\r\n\t\t// fall through to cleanup\r\n\t}\r\n\r\n\tlocalStorage.removeItem(USER_STORAGE_KEY);\r\n\r\n\treturn null;\r\n}\r\n","import { Service, computed, inject, signal } from '@angular/core';\r\nimport { Router } from '@angular/router';\r\nimport { AlertService } from '@wawjs/ngx-ui';\r\nimport { CrudService } from '@wawjs/ngx-crud';\r\nimport { EmitterService } from '@wawjs/ngx-core';\r\nimport { HttpService } from '@wawjs/ngx-http';\r\nimport { NGX_BOS_CONFIG } from '../../ngx-bos.config';\r\nimport { User } from './user.interface';\r\nimport { readStoredUser, USER_STORAGE_KEY } from './user.storage';\r\n@Service()\r\nexport class UserService extends CrudService<User> {\r\n\tprivate readonly _userHttpService = inject(HttpService);\r\n\r\n\tprivate readonly _alertService = inject(AlertService);\r\n\r\n\tprivate readonly _router = inject(Router);\r\n\r\n\tprivate readonly _emitterService = inject(EmitterService);\r\n\r\n\tprivate readonly _bosConfig = inject(NGX_BOS_CONFIG);\r\n\r\n\treadonly url = this._bosConfig.url;\r\n\r\n\troles = this._bosConfig.roles.concat(['admin']);\r\n\r\n\temployees = this._bosConfig.roles;\r\n\r\n\treadonly users = this.documents;\r\n\r\n\tuser = signal<User>(readStoredUser() ?? this.prepareDocument()());\r\n\r\n\tthumb = computed(() => {\r\n\t\tconst thumb = this.user().thumb;\r\n\r\n\t\treturn !thumb || thumb.includes(this._bosConfig.defaultUserThumb)\r\n\t\t\t? this._bosConfig.defaultUserThumb\r\n\t\t\t: /^https?:\\/\\//i.test(thumb)\r\n\t\t\t\t? thumb\r\n\t\t\t\t: this.url + thumb;\r\n\t});\r\n\r\n\trole(role: string): boolean {\r\n\t\treturn !!(this.user().is || {})[role];\r\n\t}\r\n\r\n\treadonly usersByRole = computed<Record<string, User[]>>(() => {\r\n\t\tconst splitted: Record<string, User[]> = {};\r\n\r\n\t\tfor (const user of this.documents()) {\r\n\t\t\tfor (const role of user.roles || []) {\r\n\t\t\t\tif (role === undefined || role === null) continue;\r\n\t\t\t\t(splitted[String(role)] ??= []).push(user);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn splitted;\r\n\t});\r\n\r\n\tconstructor() {\r\n\t\tsuper({\r\n\t\t\tname: 'user',\r\n\t\t\treplace: (user) => {\r\n\t\t\t\tuser.roles = [];\r\n\t\t\t\tuser.data = user.data || {};\r\n\t\t\t\tuser.is = user.is || {};\r\n\r\n\t\t\t\tfor (const field of this._bosConfig.userFields) {\r\n\t\t\t\t\tuser.data[field] = user.data[field] || {};\r\n\t\t\t\t}\r\n\r\n\t\t\t\tfor (const role of this.roles) {\r\n\t\t\t\t\tif (user.is[role]) {\r\n\t\t\t\t\t\tuser.roles.push(role);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t\t\treturn user;\r\n\t\t\t},\r\n\t\t});\r\n\r\n\t\tif (localStorage.getItem(USER_STORAGE_KEY)) {\r\n\t\t\tthis.fetch({}, { name: 'me' }).subscribe((user: User) => {\r\n\t\t\t\tif (user) {\r\n\t\t\t\t\tthis.setUser(user);\r\n\t\t\t\t} else if (localStorage.getItem(USER_STORAGE_KEY)) {\r\n\t\t\t\t\tthis.logout();\r\n\t\t\t\t}\r\n\t\t\t});\r\n\r\n\t\t\tthis.get({\r\n\t\t\t\tquery: this._bosConfig.appId ? 'appId=' + this._bosConfig.appId : '',\r\n\t\t\t});\r\n\t\t}\r\n\t}\r\n\r\n\tsetUser(user: User) {\r\n\t\tthis.user.set(user);\r\n\r\n\t\tlocalStorage.setItem(USER_STORAGE_KEY, JSON.stringify(user));\r\n\r\n\t\tthis._emitterService.complete('us.user');\r\n\t}\r\n\r\n\tupdateMe() {\r\n\t\tthis.setUser(this.user());\r\n\r\n\t\treturn this.update(this.user()).subscribe(() => {\r\n\t\t\tthis._alertService.info({\r\n\t\t\t\ttext: 'Profile information has been updated',\r\n\t\t\t});\r\n\t\t});\r\n\t}\r\n\r\n\tupdateMeAfterWhile() {\r\n\t\tthis.setUser(this.user());\r\n\r\n\t\tthis.updateAfterWhile(this.user());\r\n\t}\r\n\r\n\tchangePassword(oldPass: string, newPass: string) {\r\n\t\treturn this._userHttpService.post(\r\n\t\t\t'/api/user/changePassword',\r\n\t\t\t{\r\n\t\t\t\tnewPass: newPass,\r\n\t\t\t\toldPass: oldPass,\r\n\t\t\t},\r\n\t\t\t(resp: boolean) => {\r\n\t\t\t\tif (resp) {\r\n\t\t\t\t\tthis._alertService.info({\r\n\t\t\t\t\t\ttext: 'Successfully changed password',\r\n\t\t\t\t\t});\r\n\t\t\t\t} else {\r\n\t\t\t\t\tthis._alertService.error({\r\n\t\t\t\t\t\ttext: 'Incorrect current password',\r\n\t\t\t\t\t});\r\n\t\t\t\t}\r\n\t\t\t},\r\n\t\t);\r\n\t}\r\n\r\n\tlogout() {\r\n\t\tthis.user.set(this.prepareDocument()());\r\n\r\n\t\tlocalStorage.removeItem(USER_STORAGE_KEY);\r\n\r\n\t\t// Reload only after the server has cleared the session cookie: reloading right away can\r\n\t\t// cancel the request and leave the session alive. Do not wait longer than 3s if the API is down.\r\n\t\tlet finished = false;\r\n\r\n\t\tconst finish = () => {\r\n\t\t\tif (finished) return;\r\n\r\n\t\t\tfinished = true;\r\n\r\n\t\t\tthis._router.navigateByUrl(this._bosConfig.signPath);\r\n\r\n\t\t\tsetTimeout(() => {\r\n\t\t\t\tlocation.reload();\r\n\t\t\t}, 100);\r\n\t\t};\r\n\r\n\t\tthis._userHttpService.get('/api/user/logout', finish, { err: finish });\r\n\r\n\t\tthis._userHttpService.remove('token');\r\n\r\n\t\tsetTimeout(finish, 3000);\r\n\t}\r\n\r\n\tupdateAdmin(user: User) {\r\n\t\treturn this.update(user, {\r\n\t\t\tname: 'admin',\r\n\t\t});\r\n\t}\r\n\r\n\tdeleteAdmin(user: User) {\r\n\t\treturn this.delete(user, {\r\n\t\t\tname: 'admin',\r\n\t\t});\r\n\t}\r\n}\r\n"],"mappings":";;;;;;;;AAIA,MAAa,4BACZ,WAAqC,CAAA,OAChC;CACL,QAAQ;CACR,OAAO;CACP,YAAY;EACX;GACC,MAAM;GACN,KAAK;GACL,SAAS;GACT,OAAO;IACN,aAAa;IACb,OAAO;GACP;EACD;EACD;GACC,MAAM;GACN,KAAK;GACL,OAAO;IACN,aAAa;IACb,OAAO;IACP,MAAM;GACN;EACD;EACD;GACC,MAAM;GACN,KAAK;GACL,OAAO;IACN,aAAa;IACb,OAAO;GACP;EACD;EACD;GACC,MAAM;GACN,KAAK;GACL,OAAO;IACN,aAAa;IACb,OAAO;IACP,MAAM;GACN;EACD;EACD,GAAG;CACH;AACD;AAED,MAAa,kCAAiC;CAC7C,MAAM,SAAS,OAAO,cAAc;CAEpC,OAAO,yBAAyB,OAAO,QAAQ;AAChD;AClDA,MAAa,mBAAmB;AAOhC,SAAgB,iBAAc;CAC7B,MAAM,SAAS,aAAa,QAAQ,gBAAgB;CAEpD,IAAI,CAAC,QACJ,OAAO;CAGR,IAAI;EACH,MAAM,OAAO,KAAK,MAAM,MAAM;EAE9B,IAAI,QAAQ,OAAO,SAAS,UAC3B,OAAO;CAET,QAAQ,CAER;CAEA,aAAa,WAAW,gBAAgB;CAExC,OAAO;AACR;ACpBA,IAAa,cAAb,MAAa,oBAAoB,YAAiB;CA+BjD,KAAK,MAAY;EAChB,OAAO,CAAC,EAAE,KAAK,KAAI,CAAE,CAAC,MAAM,CAAA,EAAA,CAAI;CACjC;CAeA,cAAA;EACC,MAAM;GACL,MAAM;GACN,UAAU,SAAQ;IACjB,KAAK,QAAQ,CAAA;IACb,KAAK,OAAO,KAAK,QAAQ,CAAA;IACzB,KAAK,KAAK,KAAK,MAAM,CAAA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