@techextensor/tab-core-utility 2.1.5 → 2.1.7
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/.eslintrc.json +92 -0
- package/README.md +7 -0
- package/jest.config.ts +22 -0
- package/ng-package.json +7 -0
- package/package.json +18 -31
- package/project.json +42 -0
- package/{index.d.ts → src/index.ts} +77 -53
- package/src/lib/tab-core-utility/app/constants/api-constants.ts +33 -0
- package/src/lib/tab-core-utility/app/constants/common.ts +34 -0
- package/src/lib/tab-core-utility/app/constants/error-messages.ts +21 -0
- package/src/lib/tab-core-utility/app/constants/storage-constants.ts +17 -0
- package/src/lib/tab-core-utility/app/crud/tab-delete.service.ts +144 -0
- package/src/lib/tab-core-utility/app/crud/tab-get.service.ts +160 -0
- package/src/lib/tab-core-utility/app/crud/tab-insert.service.ts +75 -0
- package/src/lib/tab-core-utility/app/crud/tab-update.service.ts +105 -0
- package/src/lib/tab-core-utility/app/crud/tab-workflow.service.ts +39 -0
- package/src/lib/tab-core-utility/app/enums/controllers.enum.ts +24 -0
- package/src/lib/tab-core-utility/app/enums/dsq.enum.ts +46 -0
- package/src/lib/tab-core-utility/app/enums/permission.enum.ts +18 -0
- package/src/lib/tab-core-utility/app/enums/query.enum.ts +12 -0
- package/src/lib/tab-core-utility/app/enums/workflow.enum.ts +35 -0
- package/src/lib/tab-core-utility/app/helpers/common/app.helpers.ts +65 -0
- package/src/lib/tab-core-utility/app/helpers/common/dsq.helpers.ts +80 -0
- package/src/lib/tab-core-utility/app/helpers/common/req-res.helpers.ts +75 -0
- package/src/lib/tab-core-utility/app/helpers/common/where-clause.helpers.ts +262 -0
- package/src/lib/tab-core-utility/app/helpers/screen/forms.helpers.ts +33 -0
- package/src/lib/tab-core-utility/app/helpers/screen/grid-column-data.helpers.ts +88 -0
- package/src/lib/tab-core-utility/app/helpers/screen/grid.helpers.ts +113 -0
- package/src/lib/tab-core-utility/app/helpers/screen/media-upload.helpers.ts +74 -0
- package/src/lib/tab-core-utility/app/helpers/screen/plugin.helpers.ts +52 -0
- package/src/lib/tab-core-utility/app/helpers/screen/screen.helpers.ts +20 -0
- package/{lib/tab-core-utility/app/interfaces/auth.interface.d.ts → src/lib/tab-core-utility/app/interfaces/auth.interface.ts} +18 -15
- package/src/lib/tab-core-utility/app/interfaces/common-api.interface.ts +27 -0
- package/src/lib/tab-core-utility/app/interfaces/grid.interface.ts +118 -0
- package/src/lib/tab-core-utility/app/interfaces/orgInfo.interface.ts +34 -0
- package/src/lib/tab-core-utility/app/interfaces/schema.interface.ts +11 -0
- package/src/lib/tab-core-utility/app/interfaces/user.interface.ts +91 -0
- package/src/lib/tab-core-utility/app/models/api-query.class.ts +92 -0
- package/src/lib/tab-core-utility/app/models/app-object.class.ts +87 -0
- package/src/lib/tab-core-utility/app/models/common.class.ts +30 -0
- package/src/lib/tab-core-utility/app/models/data-source-queries.class.ts +114 -0
- package/src/lib/tab-core-utility/app/utils/filter.util.ts +78 -0
- package/src/lib/tab-core-utility/app/utils/form-builder.util.ts +31 -0
- package/src/lib/tab-core-utility/app/utils/json.util.ts +137 -0
- package/src/lib/tab-core-utility/app/utils/operators.util.ts +129 -0
- package/src/lib/tab-core-utility/app/utils/req-res.util.ts +189 -0
- package/src/lib/tab-core-utility/app/utils/tab-app.util.ts +131 -0
- package/src/lib/tab-core-utility/auth/auth.service.spec.ts +167 -0
- package/src/lib/tab-core-utility/auth/auth.service.ts +91 -0
- package/src/lib/tab-core-utility/auth/idb-storage.service.ts +77 -0
- package/src/lib/tab-core-utility/auth/local-storage.service.spec.ts +47 -0
- package/src/lib/tab-core-utility/auth/local-storage.service.ts +46 -0
- package/src/lib/tab-core-utility/auth/session-storage.service.spec.ts +47 -0
- package/src/lib/tab-core-utility/auth/session-storage.service.ts +46 -0
- package/src/lib/tab-core-utility/core/error-handler/error-handler-interceptor.service.ts +32 -0
- package/src/lib/tab-core-utility/core/error-handler/error-handler.store.spec.ts +16 -0
- package/src/lib/tab-core-utility/core/error-handler/error-handler.store.ts +26 -0
- package/src/lib/tab-core-utility/core/error-handler/models/error-handler.state.ts +9 -0
- package/src/lib/tab-core-utility/core/http-client/api-url.token.ts +4 -0
- package/src/lib/tab-core-utility/core/http-client/api.service.spec.ts +18 -0
- package/src/lib/tab-core-utility/core/http-client/api.service.ts +105 -0
- package/src/lib/tab-core-utility/core/util/auth.utils.ts +22 -0
- package/src/lib/tab-core-utility/store/auth.store.ts +54 -0
- package/src/lib/tab-core-utility/store/orginfo.store.ts +57 -0
- package/src/lib/tab-core-utility/store/schema.store.ts +85 -0
- package/src/test-setup.ts +8 -0
- package/tsconfig.json +29 -0
- package/tsconfig.lib.json +17 -0
- package/tsconfig.lib.prod.json +7 -0
- package/tsconfig.spec.json +16 -0
- package/esm2022/index.mjs +0 -64
- package/esm2022/lib/tab-core-utility/app/constants/api-constants.mjs +0 -33
- package/esm2022/lib/tab-core-utility/app/constants/common.mjs +0 -23
- package/esm2022/lib/tab-core-utility/app/constants/error-messages.mjs +0 -13
- package/esm2022/lib/tab-core-utility/app/constants/storage-constants.mjs +0 -15
- package/esm2022/lib/tab-core-utility/app/crud/tab-delete.service.mjs +0 -145
- package/esm2022/lib/tab-core-utility/app/crud/tab-get.service.mjs +0 -131
- package/esm2022/lib/tab-core-utility/app/crud/tab-insert.service.mjs +0 -77
- package/esm2022/lib/tab-core-utility/app/crud/tab-update.service.mjs +0 -105
- package/esm2022/lib/tab-core-utility/app/crud/tab-workflow.service.mjs +0 -43
- package/esm2022/lib/tab-core-utility/app/enums/controllers.enum.mjs +0 -26
- package/esm2022/lib/tab-core-utility/app/enums/dsq.enum.mjs +0 -48
- package/esm2022/lib/tab-core-utility/app/enums/permission.enum.mjs +0 -20
- package/esm2022/lib/tab-core-utility/app/enums/query.enum.mjs +0 -14
- package/esm2022/lib/tab-core-utility/app/enums/workflow.enum.mjs +0 -37
- package/esm2022/lib/tab-core-utility/app/helpers/common/app.helpers.mjs +0 -68
- package/esm2022/lib/tab-core-utility/app/helpers/common/dsq.helpers.mjs +0 -70
- package/esm2022/lib/tab-core-utility/app/helpers/common/req-res.helpers.mjs +0 -64
- package/esm2022/lib/tab-core-utility/app/helpers/common/where-clause.helpers.mjs +0 -208
- package/esm2022/lib/tab-core-utility/app/helpers/screen/forms.helpers.mjs +0 -38
- package/esm2022/lib/tab-core-utility/app/helpers/screen/grid-column-data.helpers.mjs +0 -78
- package/esm2022/lib/tab-core-utility/app/helpers/screen/grid.helpers.mjs +0 -112
- package/esm2022/lib/tab-core-utility/app/helpers/screen/media-upload.helpers.mjs +0 -77
- package/esm2022/lib/tab-core-utility/app/helpers/screen/plugin.helpers.mjs +0 -55
- package/esm2022/lib/tab-core-utility/app/helpers/screen/screen.helpers.mjs +0 -26
- package/esm2022/lib/tab-core-utility/app/interfaces/auth.interface.mjs +0 -2
- package/esm2022/lib/tab-core-utility/app/interfaces/common-api.interface.mjs +0 -2
- package/esm2022/lib/tab-core-utility/app/interfaces/grid.interface.mjs +0 -3
- package/esm2022/lib/tab-core-utility/app/interfaces/orgInfo.interface.mjs +0 -2
- package/esm2022/lib/tab-core-utility/app/interfaces/schema.interface.mjs +0 -2
- package/esm2022/lib/tab-core-utility/app/interfaces/user.interface.mjs +0 -2
- package/esm2022/lib/tab-core-utility/app/models/api-query.class.mjs +0 -47
- package/esm2022/lib/tab-core-utility/app/models/app-object.class.mjs +0 -30
- package/esm2022/lib/tab-core-utility/app/models/common.class.mjs +0 -12
- package/esm2022/lib/tab-core-utility/app/models/data-source-queries.class.mjs +0 -31
- package/esm2022/lib/tab-core-utility/app/utils/filter.util.mjs +0 -75
- package/esm2022/lib/tab-core-utility/app/utils/form-builder.util.mjs +0 -39
- package/esm2022/lib/tab-core-utility/app/utils/json.util.mjs +0 -138
- package/esm2022/lib/tab-core-utility/app/utils/operators.util.mjs +0 -129
- package/esm2022/lib/tab-core-utility/app/utils/req-res.util.mjs +0 -147
- package/esm2022/lib/tab-core-utility/app/utils/tab-app.util.mjs +0 -99
- package/esm2022/lib/tab-core-utility/auth/auth.service.mjs +0 -74
- package/esm2022/lib/tab-core-utility/auth/idb-storage.service.mjs +0 -69
- package/esm2022/lib/tab-core-utility/auth/local-storage.service.mjs +0 -52
- package/esm2022/lib/tab-core-utility/auth/session-storage.service.mjs +0 -52
- package/esm2022/lib/tab-core-utility/core/error-handler/error-handler-interceptor.service.mjs +0 -26
- package/esm2022/lib/tab-core-utility/core/error-handler/error-handler.store.mjs +0 -21
- package/esm2022/lib/tab-core-utility/core/error-handler/models/error-handler.state.mjs +0 -5
- package/esm2022/lib/tab-core-utility/core/http-client/api-url.token.mjs +0 -4
- package/esm2022/lib/tab-core-utility/core/http-client/api.service.mjs +0 -99
- package/esm2022/lib/tab-core-utility/core/util/auth.utils.mjs +0 -21
- package/esm2022/lib/tab-core-utility/store/auth.store.mjs +0 -43
- package/esm2022/lib/tab-core-utility/store/orginfo.store.mjs +0 -42
- package/esm2022/lib/tab-core-utility/store/schema.store.mjs +0 -68
- package/esm2022/techextensor-tab-core-utility.mjs +0 -5
- package/lib/tab-core-utility/app/constants/api-constants.d.ts +0 -30
- package/lib/tab-core-utility/app/constants/common.d.ts +0 -8
- package/lib/tab-core-utility/app/constants/error-messages.d.ts +0 -6
- package/lib/tab-core-utility/app/constants/storage-constants.d.ts +0 -14
- package/lib/tab-core-utility/app/crud/tab-delete.service.d.ts +0 -51
- package/lib/tab-core-utility/app/crud/tab-get.service.d.ts +0 -58
- package/lib/tab-core-utility/app/crud/tab-insert.service.d.ts +0 -37
- package/lib/tab-core-utility/app/crud/tab-update.service.d.ts +0 -14
- package/lib/tab-core-utility/app/crud/tab-workflow.service.d.ts +0 -20
- package/lib/tab-core-utility/app/enums/controllers.enum.d.ts +0 -24
- package/lib/tab-core-utility/app/enums/dsq.enum.d.ts +0 -42
- package/lib/tab-core-utility/app/enums/permission.enum.d.ts +0 -16
- package/lib/tab-core-utility/app/enums/query.enum.d.ts +0 -11
- package/lib/tab-core-utility/app/enums/workflow.enum.d.ts +0 -30
- package/lib/tab-core-utility/app/helpers/common/app.helpers.d.ts +0 -31
- package/lib/tab-core-utility/app/helpers/common/dsq.helpers.d.ts +0 -32
- package/lib/tab-core-utility/app/helpers/common/req-res.helpers.d.ts +0 -62
- package/lib/tab-core-utility/app/helpers/common/where-clause.helpers.d.ts +0 -82
- package/lib/tab-core-utility/app/helpers/screen/forms.helpers.d.ts +0 -11
- package/lib/tab-core-utility/app/helpers/screen/grid-column-data.helpers.d.ts +0 -33
- package/lib/tab-core-utility/app/helpers/screen/grid.helpers.d.ts +0 -63
- package/lib/tab-core-utility/app/helpers/screen/media-upload.helpers.d.ts +0 -38
- package/lib/tab-core-utility/app/helpers/screen/plugin.helpers.d.ts +0 -25
- package/lib/tab-core-utility/app/helpers/screen/screen.helpers.d.ts +0 -11
- package/lib/tab-core-utility/app/interfaces/common-api.interface.d.ts +0 -24
- package/lib/tab-core-utility/app/interfaces/grid.interface.d.ts +0 -110
- package/lib/tab-core-utility/app/interfaces/orgInfo.interface.d.ts +0 -31
- package/lib/tab-core-utility/app/interfaces/schema.interface.d.ts +0 -11
- package/lib/tab-core-utility/app/interfaces/user.interface.d.ts +0 -84
- package/lib/tab-core-utility/app/models/api-query.class.d.ts +0 -69
- package/lib/tab-core-utility/app/models/app-object.class.d.ts +0 -74
- package/lib/tab-core-utility/app/models/common.class.d.ts +0 -24
- package/lib/tab-core-utility/app/models/data-source-queries.class.d.ts +0 -95
- package/lib/tab-core-utility/app/utils/filter.util.d.ts +0 -30
- package/lib/tab-core-utility/app/utils/form-builder.util.d.ts +0 -13
- package/lib/tab-core-utility/app/utils/json.util.d.ts +0 -25
- package/lib/tab-core-utility/app/utils/operators.util.d.ts +0 -21
- package/lib/tab-core-utility/app/utils/req-res.util.d.ts +0 -61
- package/lib/tab-core-utility/app/utils/tab-app.util.d.ts +0 -48
- package/lib/tab-core-utility/auth/auth.service.d.ts +0 -53
- package/lib/tab-core-utility/auth/idb-storage.service.d.ts +0 -14
- package/lib/tab-core-utility/auth/local-storage.service.d.ts +0 -22
- package/lib/tab-core-utility/auth/session-storage.service.d.ts +0 -22
- package/lib/tab-core-utility/core/error-handler/error-handler-interceptor.service.d.ts +0 -3
- package/lib/tab-core-utility/core/error-handler/error-handler.store.d.ts +0 -10
- package/lib/tab-core-utility/core/error-handler/models/error-handler.state.d.ts +0 -5
- package/lib/tab-core-utility/core/http-client/api-url.token.d.ts +0 -3
- package/lib/tab-core-utility/core/http-client/api.service.d.ts +0 -65
- package/lib/tab-core-utility/core/util/auth.utils.d.ts +0 -9
- package/lib/tab-core-utility/store/auth.store.d.ts +0 -19
- package/lib/tab-core-utility/store/orginfo.store.d.ts +0 -22
- package/lib/tab-core-utility/store/schema.store.d.ts +0 -32
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import {
|
|
3
|
+
HttpClientTestingModule,
|
|
4
|
+
HttpTestingController,
|
|
5
|
+
} from '@angular/common/http/testing';
|
|
6
|
+
import { AuthService } from './auth.service';
|
|
7
|
+
import { ApiService } from '../core/http-client/api.service';
|
|
8
|
+
import { UserResponse } from '../app/interfaces/user.interface';
|
|
9
|
+
import { ApiResponse } from '../app/interfaces/common-api.interface';
|
|
10
|
+
import { LocalStorageService } from './local-storage.service';
|
|
11
|
+
|
|
12
|
+
describe('AuthService', () => {
|
|
13
|
+
let authService: AuthService;
|
|
14
|
+
let httpMock: HttpTestingController;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
TestBed.configureTestingModule({
|
|
18
|
+
imports: [HttpClientTestingModule],
|
|
19
|
+
providers: [AuthService, ApiService, LocalStorageService],
|
|
20
|
+
});
|
|
21
|
+
authService = TestBed.inject(AuthService);
|
|
22
|
+
httpMock = TestBed.inject(HttpTestingController);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
httpMock.verify();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should be created', () => {
|
|
30
|
+
expect(authService).toBeTruthy();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should fetch user details', () => {
|
|
34
|
+
const mockUrl = 'mockUrl';
|
|
35
|
+
const mockResponse: UserResponse = {
|
|
36
|
+
/* mock user response object */
|
|
37
|
+
user: {
|
|
38
|
+
id: '1',
|
|
39
|
+
email: 'john.doe@example.com',
|
|
40
|
+
token: 'example_token',
|
|
41
|
+
username: 'john_doe',
|
|
42
|
+
bio: 'Sample biography',
|
|
43
|
+
image: 'https://example.com/profile.jpg',
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
authService.user(mockUrl).subscribe((response: UserResponse) => {
|
|
48
|
+
expect(response).toEqual(mockResponse);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const request = httpMock.expectOne(mockUrl);
|
|
52
|
+
expect(request.request.method).toBe('GET');
|
|
53
|
+
request.flush(mockResponse);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should sign in with given credentials', () => {
|
|
57
|
+
const mockUrl = 'mockUrl';
|
|
58
|
+
const mockCredentials = {
|
|
59
|
+
/* mock login credentials */
|
|
60
|
+
email: 'john.doe@example.com',
|
|
61
|
+
password: '123456',
|
|
62
|
+
};
|
|
63
|
+
const mockResponse: UserResponse = {
|
|
64
|
+
/* mock user response object */
|
|
65
|
+
user: {
|
|
66
|
+
id: '1',
|
|
67
|
+
email: 'john.doe@example.com',
|
|
68
|
+
token: 'example_token',
|
|
69
|
+
username: 'john_doe',
|
|
70
|
+
bio: 'Sample biography',
|
|
71
|
+
image: 'https://example.com/profile.jpg',
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
authService
|
|
76
|
+
.signIn(mockUrl, mockCredentials)
|
|
77
|
+
.subscribe((response: UserResponse) => {
|
|
78
|
+
expect(response).toEqual(mockResponse);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const request = httpMock.expectOne(mockUrl);
|
|
82
|
+
expect(request.request.method).toBe('POST');
|
|
83
|
+
expect(request.request.body).toEqual(mockCredentials);
|
|
84
|
+
request.flush(mockResponse);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should register new user with provided credentials', () => {
|
|
88
|
+
const mockUrl = 'mockUrl';
|
|
89
|
+
const mockCredentials = {
|
|
90
|
+
/* mock login credentials */
|
|
91
|
+
username: 'john.doe',
|
|
92
|
+
email: 'john.doe@example.com',
|
|
93
|
+
password: '123456',
|
|
94
|
+
};
|
|
95
|
+
const mockResponse: UserResponse = {
|
|
96
|
+
/* mock user response object */
|
|
97
|
+
user: {
|
|
98
|
+
id: '1',
|
|
99
|
+
email: 'john.doe@example.com',
|
|
100
|
+
token: 'example_token',
|
|
101
|
+
username: 'john_doe',
|
|
102
|
+
bio: 'Sample biography',
|
|
103
|
+
image: 'https://example.com/profile.jpg',
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
authService
|
|
108
|
+
.register(mockUrl, mockCredentials)
|
|
109
|
+
.subscribe((response: UserResponse) => {
|
|
110
|
+
expect(response).toEqual(mockResponse);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const request = httpMock.expectOne(mockUrl);
|
|
114
|
+
expect(request.request.method).toBe('POST');
|
|
115
|
+
expect(request.request.body).toEqual(mockCredentials);
|
|
116
|
+
request.flush(mockResponse);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should forgot password in with given credentials', () => {
|
|
120
|
+
const mockUrl = 'mockUrl';
|
|
121
|
+
const email = { email: 'test@example.com' };
|
|
122
|
+
const response: ApiResponse = {
|
|
123
|
+
requestId: '123456',
|
|
124
|
+
isSuccess: true,
|
|
125
|
+
result: [],
|
|
126
|
+
errors: [],
|
|
127
|
+
};
|
|
128
|
+
authService
|
|
129
|
+
.forgotPassword(mockUrl, email)
|
|
130
|
+
.subscribe((response: ApiResponse) => {
|
|
131
|
+
expect(response).toEqual(response);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const request = httpMock.expectOne(mockUrl);
|
|
135
|
+
expect(request.request.method).toBe('POST');
|
|
136
|
+
expect(request.request.body).toEqual(email);
|
|
137
|
+
request.flush(response);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should reset password in with given credentials', () => {
|
|
141
|
+
const mockUrl = 'mockUrl';
|
|
142
|
+
const password = { password: '123456' };
|
|
143
|
+
const response: ApiResponse = {
|
|
144
|
+
requestId: '123456',
|
|
145
|
+
isSuccess: true,
|
|
146
|
+
result: [],
|
|
147
|
+
errors: [],
|
|
148
|
+
};
|
|
149
|
+
authService
|
|
150
|
+
.resetPassword(mockUrl, password)
|
|
151
|
+
.subscribe((response: ApiResponse) => {
|
|
152
|
+
expect(response).toEqual(response);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const request = httpMock.expectOne(mockUrl);
|
|
156
|
+
expect(request.request.method).toBe('POST');
|
|
157
|
+
expect(request.request.body).toEqual(password);
|
|
158
|
+
request.flush(response);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should logout current session', () => {
|
|
162
|
+
spyOn(localStorage, 'removeItem');
|
|
163
|
+
const key = 'testKey';
|
|
164
|
+
authService.logout(key);
|
|
165
|
+
expect(localStorage.removeItem(key)).toHaveBeenCalled();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Injectable, inject } from '@angular/core';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import {
|
|
4
|
+
ForgotPasswordUserRequest,
|
|
5
|
+
LoginUserRequest,
|
|
6
|
+
NewUserRequest,
|
|
7
|
+
ResetPasswordUserRequest,
|
|
8
|
+
} from '../app/interfaces/auth.interface';
|
|
9
|
+
import { UserResponse } from '../app/interfaces/user.interface';
|
|
10
|
+
import { ApiService } from '../core/http-client/api.service';
|
|
11
|
+
import { ApiResponse } from '../app/interfaces/common-api.interface';
|
|
12
|
+
import { LocalStorageService } from './local-storage.service';
|
|
13
|
+
|
|
14
|
+
@Injectable({ providedIn: 'root' })
|
|
15
|
+
export class AuthService {
|
|
16
|
+
/**
|
|
17
|
+
* Injecting required services
|
|
18
|
+
*/
|
|
19
|
+
private readonly apiService = inject(ApiService);
|
|
20
|
+
private readonly localStorageService = inject(LocalStorageService);
|
|
21
|
+
|
|
22
|
+
// -----------------------------------------------------------------------------------------------------
|
|
23
|
+
// @ Public methods
|
|
24
|
+
// -----------------------------------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Fetch user details from the database
|
|
28
|
+
*
|
|
29
|
+
* @param endPointName - The End Point Name to fetch user details
|
|
30
|
+
* @returns Observable<UserResponse> - An observable of UserResponse
|
|
31
|
+
*/
|
|
32
|
+
user(endPointName: string): Observable<UserResponse> {
|
|
33
|
+
return this.apiService.get<UserResponse>(endPointName);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sign in with provided credentials
|
|
38
|
+
*
|
|
39
|
+
* @param endPointName - The End Point Name to sign in
|
|
40
|
+
* @param credentials - The login credentials
|
|
41
|
+
* @returns Observable<UserResponse> - An observable of UserResponse
|
|
42
|
+
*/
|
|
43
|
+
signIn(endPointName: string, credentials: LoginUserRequest): Observable<UserResponse> {
|
|
44
|
+
return this.apiService.post<UserResponse, LoginUserRequest>(
|
|
45
|
+
endPointName,
|
|
46
|
+
credentials
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Register a new user
|
|
52
|
+
*
|
|
53
|
+
* @param endPointName - The End Point Name to register a new user
|
|
54
|
+
* @param credentials - The new user's credentials
|
|
55
|
+
* @returns Observable<UserResponse> - An observable of UserResponse
|
|
56
|
+
*/
|
|
57
|
+
register(endPointName: string, credentials: NewUserRequest): Observable<UserResponse> {
|
|
58
|
+
return this.apiService.post<UserResponse, NewUserRequest>(endPointName, credentials);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Forgot password
|
|
63
|
+
* @param endPointName - The End Point Name of API
|
|
64
|
+
* @param email - The email of user
|
|
65
|
+
*/
|
|
66
|
+
forgotPassword(
|
|
67
|
+
endPointName: string,
|
|
68
|
+
email: ForgotPasswordUserRequest
|
|
69
|
+
): Observable<ApiResponse> {
|
|
70
|
+
return this.apiService.post(endPointName, email);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Reset password
|
|
75
|
+
* @param endPointName - The End Point Name of API
|
|
76
|
+
* @param password - The password of user
|
|
77
|
+
*/
|
|
78
|
+
resetPassword(
|
|
79
|
+
endPointName: string,
|
|
80
|
+
password: ResetPasswordUserRequest
|
|
81
|
+
): Observable<ApiResponse> {
|
|
82
|
+
return this.apiService.post(endPointName, password);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Logout current session
|
|
87
|
+
*/
|
|
88
|
+
logout(key: string) {
|
|
89
|
+
this.localStorageService.removeLocalStorage(key);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { openDB, IDBPDatabase, DBSchema } from 'idb';
|
|
3
|
+
|
|
4
|
+
interface MyDB extends DBSchema {
|
|
5
|
+
myObjectStore: {
|
|
6
|
+
key: string;
|
|
7
|
+
value: { key: string; data: string };
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@Injectable({ providedIn: 'root' })
|
|
12
|
+
export class IdbService {
|
|
13
|
+
private readonly dbName = 'TabDatabase';
|
|
14
|
+
private readonly dbVersion = 1;
|
|
15
|
+
private dbPromise: Promise<IDBPDatabase<MyDB>>;
|
|
16
|
+
|
|
17
|
+
constructor() {
|
|
18
|
+
this.dbPromise = openDB<MyDB>(this.dbName, this.dbVersion, {
|
|
19
|
+
upgrade(db) {
|
|
20
|
+
if (!db.objectStoreNames.contains('myObjectStore')) {
|
|
21
|
+
db.createObjectStore('myObjectStore', { keyPath: 'key' });
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async setItem(key: string, data: any): Promise<void> {
|
|
28
|
+
try {
|
|
29
|
+
if (data) {
|
|
30
|
+
await this.setItemIndexedDb(key, data);
|
|
31
|
+
}
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw new Error('Error storing data: ' + error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private async setItemIndexedDb(key: string, data: any): Promise<void> {
|
|
38
|
+
const db = await this.dbPromise;
|
|
39
|
+
const tx = db.transaction('myObjectStore', 'readwrite');
|
|
40
|
+
const store = tx.objectStore('myObjectStore');
|
|
41
|
+
await store.put({ key, data });
|
|
42
|
+
await tx.done;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async getItem(key: string): Promise<any> {
|
|
46
|
+
try {
|
|
47
|
+
const result = await this.getItemIndexedDb(key);
|
|
48
|
+
return result?.data;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
throw new Error('Error retrieving data: ' + error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private async getItemIndexedDb(key: string): Promise<any> {
|
|
55
|
+
const db = await this.dbPromise;
|
|
56
|
+
const tx = db.transaction('myObjectStore', 'readonly');
|
|
57
|
+
const store = tx.objectStore('myObjectStore');
|
|
58
|
+
const result = await store.get(key);
|
|
59
|
+
await tx.done;
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async clearIndexedDB(): Promise<void> {
|
|
64
|
+
try {
|
|
65
|
+
const db = await this.dbPromise;
|
|
66
|
+
const tx = db.transaction('myObjectStore', 'readwrite');
|
|
67
|
+
const store = tx.objectStore('myObjectStore');
|
|
68
|
+
await store.clear();
|
|
69
|
+
await tx.done;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new Error('Error clearing IndexedDB: ' + error)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { TestBed, inject } from '@angular/core/testing';
|
|
2
|
+
import { LocalStorageService } from './local-storage.service';
|
|
3
|
+
|
|
4
|
+
describe('LocalStorageService', () => {
|
|
5
|
+
let localStorageService: LocalStorageService;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
TestBed.configureTestingModule({
|
|
9
|
+
providers: [LocalStorageService],
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should be created', () => {
|
|
14
|
+
inject([LocalStorageService], (service: LocalStorageService) => {
|
|
15
|
+
expect(service).toBeTruthy();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should set data in local storage', () => {
|
|
20
|
+
const key = 'testKey';
|
|
21
|
+
const data = { name: 'John Doe', age: 30 };
|
|
22
|
+
localStorageService.setLocalStorage(key, data);
|
|
23
|
+
expect(localStorage.getItem(key)).toEqual(JSON.stringify(data));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should get data from local storage', () => {
|
|
27
|
+
const key = 'testKey';
|
|
28
|
+
const data = { name: 'Jane Smith', age: 25 };
|
|
29
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
30
|
+
const retrievedData = localStorageService.getLocalStorage(key);
|
|
31
|
+
expect(retrievedData).toEqual(data);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should return null when getting non-existent data from local storage', () => {
|
|
35
|
+
const key = 'nonExistentKey';
|
|
36
|
+
const retrievedData = localStorageService.getLocalStorage(key);
|
|
37
|
+
expect(retrievedData).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should remove data from local storage', () => {
|
|
41
|
+
const key = 'testKey';
|
|
42
|
+
const data = { city: 'New York', population: 8000000 };
|
|
43
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
44
|
+
localStorageService.removeLocalStorage(key);
|
|
45
|
+
expect(localStorage.getItem(key)).toBeNull();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { Injectable } from '@angular/core';
|
|
3
|
+
|
|
4
|
+
@Injectable({ providedIn: 'root' })
|
|
5
|
+
export class LocalStorageService {
|
|
6
|
+
// -----------------------------------------------------------------------------------------------------
|
|
7
|
+
// @ Public methods
|
|
8
|
+
// -----------------------------------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Set data in local storage
|
|
12
|
+
* @param key - Key to store the data
|
|
13
|
+
* @param data - Data to be stored
|
|
14
|
+
*/
|
|
15
|
+
setLocalStorage = (key: string, data: any): void => {
|
|
16
|
+
// localStorage.setItem(key, JSON.stringify(data));
|
|
17
|
+
if (typeof data === 'string') {
|
|
18
|
+
localStorage.setItem(key, data);
|
|
19
|
+
} else {
|
|
20
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get data from local storage
|
|
26
|
+
* @param key - Key to retrieve the data
|
|
27
|
+
* @returns The data stored in local storage
|
|
28
|
+
*/
|
|
29
|
+
getLocalStorage = (key: string): any => {
|
|
30
|
+
const data = localStorage.getItem(key);
|
|
31
|
+
return data ? data: null;
|
|
32
|
+
// if (typeof data === 'string') {
|
|
33
|
+
// return data ? data: null;
|
|
34
|
+
// } else {
|
|
35
|
+
// return data ? JSON.parse(data) : null;
|
|
36
|
+
// }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Remove data from local storage
|
|
41
|
+
* @param key - Key of the data to be removed
|
|
42
|
+
*/
|
|
43
|
+
removeLocalStorage = (key: string): void => {
|
|
44
|
+
localStorage.removeItem(key);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { TestBed, inject } from '@angular/core/testing';
|
|
2
|
+
import { SessionStorageService } from './session-storage.service';
|
|
3
|
+
|
|
4
|
+
describe('SessionStorageService', () => {
|
|
5
|
+
let sessionStorageService: SessionStorageService;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
TestBed.configureTestingModule({
|
|
9
|
+
providers: [SessionStorageService],
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should be created', () => {
|
|
14
|
+
inject([SessionStorageService], (service: SessionStorageService) => {
|
|
15
|
+
expect(service).toBeTruthy();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should set data in session storage', () => {
|
|
20
|
+
const key = 'testKey';
|
|
21
|
+
const data = { name: 'John Doe', age: 30 };
|
|
22
|
+
sessionStorageService.setSessionStorage(key, data);
|
|
23
|
+
expect(sessionStorage.getItem(key)).toEqual(JSON.stringify(data));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should get data from session storage', () => {
|
|
27
|
+
const key = 'testKey';
|
|
28
|
+
const data = { name: 'Jane Smith', age: 25 };
|
|
29
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
30
|
+
const retrievedData = sessionStorageService.getSessionStorage(key);
|
|
31
|
+
expect(retrievedData).toEqual(data);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should return null when getting non-existent data from session storage', () => {
|
|
35
|
+
const key = 'nonExistentKey';
|
|
36
|
+
const retrievedData = sessionStorageService.getSessionStorage(key);
|
|
37
|
+
expect(retrievedData).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should remove data from session storage', () => {
|
|
41
|
+
const key = 'testKey';
|
|
42
|
+
const data = { city: 'New York', population: 8000000 };
|
|
43
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
44
|
+
sessionStorageService.removeSessionStorage(key);
|
|
45
|
+
expect(sessionStorage.getItem(key)).toBeNull();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { Injectable } from '@angular/core';
|
|
3
|
+
|
|
4
|
+
@Injectable({ providedIn: 'root' })
|
|
5
|
+
export class SessionStorageService {
|
|
6
|
+
// -----------------------------------------------------------------------------------------------------
|
|
7
|
+
// @ Public methods
|
|
8
|
+
// -----------------------------------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Set data in session storage
|
|
12
|
+
* @param key - Key to store the data
|
|
13
|
+
* @param data - Data to be stored
|
|
14
|
+
*/
|
|
15
|
+
setSessionStorage = (key: string, data: any): void => {
|
|
16
|
+
// sessionStorage.setItem(key, JSON.stringify(data));
|
|
17
|
+
if (typeof data === 'string') {
|
|
18
|
+
sessionStorage.setItem(key, data);
|
|
19
|
+
} else {
|
|
20
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get data from session storage
|
|
26
|
+
* @param key - Key to retrieve the data
|
|
27
|
+
* @returns The data stored in session storage
|
|
28
|
+
*/
|
|
29
|
+
getSessionStorage = (key: string): any => {
|
|
30
|
+
const data = sessionStorage.getItem(key);
|
|
31
|
+
return data ? data: null;
|
|
32
|
+
// if (typeof data === 'string') {
|
|
33
|
+
// return data ? data: null;
|
|
34
|
+
// } else {
|
|
35
|
+
// return data ? JSON.parse(data) : null;
|
|
36
|
+
// }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Remove data from session storage
|
|
41
|
+
* @param key - Key of the data to be removed
|
|
42
|
+
*/
|
|
43
|
+
removeSessionStorage = (key: string): void => {
|
|
44
|
+
sessionStorage.removeItem(key);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { HttpErrorResponse, HttpEvent, HttpHandlerFn, HttpRequest } from '@angular/common/http';
|
|
3
|
+
import { inject } from '@angular/core';
|
|
4
|
+
import { Observable, throwError } from 'rxjs';
|
|
5
|
+
import { catchError } from 'rxjs/operators';
|
|
6
|
+
import { ErrorHandlerStore } from './error-handler.store';
|
|
7
|
+
|
|
8
|
+
export const errorHandlingInterceptor = (
|
|
9
|
+
request: HttpRequest<any>,
|
|
10
|
+
next: HttpHandlerFn,
|
|
11
|
+
): Observable<HttpEvent<any>> => {
|
|
12
|
+
const errorHandlerStore = inject(ErrorHandlerStore);
|
|
13
|
+
|
|
14
|
+
return next(request).pipe(
|
|
15
|
+
catchError((error) => {
|
|
16
|
+
if (error instanceof HttpErrorResponse) {
|
|
17
|
+
switch (error.status) {
|
|
18
|
+
case 401:
|
|
19
|
+
errorHandlerStore.handleError401(error);
|
|
20
|
+
break;
|
|
21
|
+
case 404:
|
|
22
|
+
errorHandlerStore.handleError404(error);
|
|
23
|
+
break;
|
|
24
|
+
default:
|
|
25
|
+
throwError(() => error);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return throwError(() => error);
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { inject, TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { ErrorHandlerStore } from './error-handler.store';
|
|
4
|
+
import { provideMockStore } from '@ngrx/store/testing';
|
|
5
|
+
|
|
6
|
+
describe('ErrorHandlerStore', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
TestBed.configureTestingModule({
|
|
9
|
+
providers: [ErrorHandlerStore, provideMockStore({})],
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should be created', inject([ErrorHandlerStore], (service: typeof ErrorHandlerStore) => {
|
|
14
|
+
expect(service).toBeTruthy();
|
|
15
|
+
}));
|
|
16
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { signalStore, withState, withMethods, patchState } from '@ngrx/signals';
|
|
2
|
+
import { inject } from '@angular/core';
|
|
3
|
+
import { Router } from '@angular/router';
|
|
4
|
+
import { ErrorHandlerState, errorHandlerInitialState } from './models/error-handler.state';
|
|
5
|
+
import { HttpErrorResponse } from '@angular/common/http';
|
|
6
|
+
|
|
7
|
+
export const ErrorHandlerStore = signalStore(
|
|
8
|
+
{ providedIn: 'root' },
|
|
9
|
+
withState<ErrorHandlerState>(errorHandlerInitialState),
|
|
10
|
+
withMethods((store, router = inject(Router)) => ({
|
|
11
|
+
handleError401: (error: HttpErrorResponse) => {
|
|
12
|
+
patchState(store, {
|
|
13
|
+
code: error.status,
|
|
14
|
+
message: error.message,
|
|
15
|
+
});
|
|
16
|
+
router.navigate(['/login']);
|
|
17
|
+
},
|
|
18
|
+
handleError404: (error: HttpErrorResponse) => {
|
|
19
|
+
patchState(store, {
|
|
20
|
+
code: error.status,
|
|
21
|
+
message: error.message,
|
|
22
|
+
});
|
|
23
|
+
router.navigate(['/']);
|
|
24
|
+
},
|
|
25
|
+
})),
|
|
26
|
+
);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TestBed, inject } from '@angular/core/testing';
|
|
2
|
+
import { HttpClientTestingModule } from '@angular/common/http/testing';
|
|
3
|
+
|
|
4
|
+
import { ApiService } from './api.service';
|
|
5
|
+
import { API_URL } from './api-url.token';
|
|
6
|
+
|
|
7
|
+
describe('ApiService', () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
TestBed.configureTestingModule({
|
|
10
|
+
imports: [HttpClientTestingModule],
|
|
11
|
+
providers: [ApiService, { provide: API_URL, useValue: 'api_url' }],
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should be created', inject([ApiService], (service: ApiService) => {
|
|
16
|
+
expect(service).toBeTruthy();
|
|
17
|
+
}));
|
|
18
|
+
});
|