@sneat/space-services 0.1.0
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/eslint.config.js +7 -0
- package/ng-package.json +7 -0
- package/package.json +17 -0
- package/project.json +38 -0
- package/src/index.ts +8 -0
- package/src/lib/components/with-space-input.directive.spec.ts +41 -0
- package/src/lib/components/with-space-input.directive.ts +40 -0
- package/src/lib/services/space-context.service.spec.ts +16 -0
- package/src/lib/services/space-context.service.ts +45 -0
- package/src/lib/services/space-item.service.spec.ts +179 -0
- package/src/lib/services/space-item.service.ts +308 -0
- package/src/lib/services/space-module.service.spec.ts +48 -0
- package/src/lib/services/space-module.service.ts +69 -0
- package/src/lib/services/space-nav-service.service.spec.ts +32 -0
- package/src/lib/services/space-nav.service.ts +175 -0
- package/src/lib/services/space-service.module.spec.ts +46 -0
- package/src/lib/services/space-service.module.ts +7 -0
- package/src/lib/services/space.service.spec.ts +44 -0
- package/src/lib/services/space.service.ts +291 -0
- package/src/test-setup.ts +3 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +19 -0
- package/tsconfig.lib.prod.json +7 -0
- package/tsconfig.spec.json +31 -0
- package/vite.config.mts +10 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { Firestore } from '@angular/fire/firestore';
|
|
3
|
+
import { SneatApiService } from '@sneat/api';
|
|
4
|
+
import { SneatAuthStateService, SneatUserService } from '@sneat/auth-core';
|
|
5
|
+
import { ErrorLogger } from '@sneat/core';
|
|
6
|
+
import { of } from 'rxjs';
|
|
7
|
+
import { SpaceService } from './space.service';
|
|
8
|
+
|
|
9
|
+
describe('SpaceService', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
TestBed.configureTestingModule({
|
|
12
|
+
providers: [
|
|
13
|
+
SpaceService,
|
|
14
|
+
{
|
|
15
|
+
provide: ErrorLogger,
|
|
16
|
+
useValue: { logError: vi.fn(), logErrorHandler: () => vi.fn() },
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
provide: Firestore,
|
|
20
|
+
useValue: { type: 'Firestore', toJSON: () => ({}) },
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
provide: SneatUserService,
|
|
24
|
+
useValue: { userState: of({ record: undefined }) },
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
provide: SneatApiService,
|
|
28
|
+
useValue: { post: vi.fn(), get: vi.fn() },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
provide: SneatAuthStateService,
|
|
32
|
+
useValue: {
|
|
33
|
+
authStatus: of('notAuthenticated'),
|
|
34
|
+
authState: of({ status: 'notAuthenticated' }),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should be created', () => {
|
|
42
|
+
expect(TestBed.inject(SpaceService)).toBeTruthy();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { HttpParams } from '@angular/common/http';
|
|
2
|
+
import {
|
|
3
|
+
Injectable,
|
|
4
|
+
Injector,
|
|
5
|
+
inject,
|
|
6
|
+
runInInjectionContext,
|
|
7
|
+
} from '@angular/core';
|
|
8
|
+
import {
|
|
9
|
+
Firestore as AngularFirestore,
|
|
10
|
+
CollectionReference,
|
|
11
|
+
collection,
|
|
12
|
+
} from '@angular/fire/firestore';
|
|
13
|
+
import { SneatApiService, SneatFirestoreService } from '@sneat/api';
|
|
14
|
+
import {
|
|
15
|
+
AuthStatus,
|
|
16
|
+
AuthStatuses,
|
|
17
|
+
SneatAuthStateService,
|
|
18
|
+
} from '@sneat/auth-core';
|
|
19
|
+
import { IUserSpaceBrief } from '@sneat/auth-models';
|
|
20
|
+
import { IIdAndBrief } from '@sneat/core';
|
|
21
|
+
import { IRecord } from '@sneat/data';
|
|
22
|
+
import { ISpaceBrief, ISpaceDbo, ISpaceMetric } from '@sneat/dto';
|
|
23
|
+
import { ErrorLogger, IErrorLogger } from '@sneat/core';
|
|
24
|
+
import {
|
|
25
|
+
ICreateSpaceRequest,
|
|
26
|
+
ICreateSpaceResponse,
|
|
27
|
+
ILeaveSpaceRequest,
|
|
28
|
+
ISpaceContext,
|
|
29
|
+
IUpdateRelatedRequest,
|
|
30
|
+
zipMapBriefsWithIDs,
|
|
31
|
+
} from '@sneat/space-models';
|
|
32
|
+
import { ISneatUserState, SneatUserService } from '@sneat/auth-core';
|
|
33
|
+
import {
|
|
34
|
+
BehaviorSubject,
|
|
35
|
+
Observable,
|
|
36
|
+
Subject,
|
|
37
|
+
Subscription,
|
|
38
|
+
throwError,
|
|
39
|
+
} from 'rxjs';
|
|
40
|
+
import { filter, first, map, tap } from 'rxjs/operators';
|
|
41
|
+
|
|
42
|
+
const spaceBriefFromUserSpaceInfo = (v: IUserSpaceBrief): ISpaceBrief => ({
|
|
43
|
+
...v,
|
|
44
|
+
type: v.type,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// export class CachedDataService<Brief, Dbo extends Brief> {
|
|
48
|
+
// constructor(
|
|
49
|
+
// private readonly db: AngularFirestore,
|
|
50
|
+
// ) {
|
|
51
|
+
// }
|
|
52
|
+
//
|
|
53
|
+
// // watchRecord()
|
|
54
|
+
// }
|
|
55
|
+
|
|
56
|
+
@Injectable()
|
|
57
|
+
export class SpaceService {
|
|
58
|
+
readonly sneatAuthStateService = inject(SneatAuthStateService);
|
|
59
|
+
private readonly errorLogger = inject<IErrorLogger>(ErrorLogger);
|
|
60
|
+
private readonly afs = inject(AngularFirestore);
|
|
61
|
+
private readonly userService = inject(SneatUserService);
|
|
62
|
+
private readonly sneatApiService = inject(SneatApiService);
|
|
63
|
+
|
|
64
|
+
private userID?: string;
|
|
65
|
+
|
|
66
|
+
private currentUserSpaces?: Record<string, IUserSpaceBrief>;
|
|
67
|
+
|
|
68
|
+
private spaces$: Record<string, BehaviorSubject<ISpaceContext | undefined>> =
|
|
69
|
+
{};
|
|
70
|
+
private subscriptions: Subscription[] = [];
|
|
71
|
+
|
|
72
|
+
private readonly sfs: SneatFirestoreService<ISpaceBrief, ISpaceDbo>;
|
|
73
|
+
|
|
74
|
+
private readonly injector = inject(Injector);
|
|
75
|
+
|
|
76
|
+
constructor() {
|
|
77
|
+
const sneatAuthStateService = this.sneatAuthStateService;
|
|
78
|
+
|
|
79
|
+
// console.log('SpaceService.constructor()');
|
|
80
|
+
this.sfs = new SneatFirestoreService<ISpaceBrief, ISpaceDbo>(
|
|
81
|
+
this.injector,
|
|
82
|
+
(id: string, dto: ISpaceDbo) => ({
|
|
83
|
+
id,
|
|
84
|
+
...dto,
|
|
85
|
+
}),
|
|
86
|
+
);
|
|
87
|
+
const onAuthStatusChanged = (status: AuthStatus): void => {
|
|
88
|
+
if (status === 'notAuthenticated') {
|
|
89
|
+
this.unsubscribe('signed out');
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
sneatAuthStateService.authStatus.subscribe(onAuthStatusChanged);
|
|
93
|
+
|
|
94
|
+
// We are intentionally not un-subscribing from user record updates. TODO: why?
|
|
95
|
+
this.userService.userState.subscribe({
|
|
96
|
+
next: this.processUserRecordInSpaceService,
|
|
97
|
+
error: this.errorLogger.logErrorHandler('failed to load user record'),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private readonly processUserRecordInSpaceService = (
|
|
102
|
+
userState: ISneatUserState,
|
|
103
|
+
): void => {
|
|
104
|
+
const user = userState?.record;
|
|
105
|
+
if (!user) {
|
|
106
|
+
// this.userID = undefined;
|
|
107
|
+
if (
|
|
108
|
+
userState.status === AuthStatuses.notAuthenticated &&
|
|
109
|
+
this.subscriptions?.length
|
|
110
|
+
) {
|
|
111
|
+
this.unsubscribe(
|
|
112
|
+
'user is not authenticated and active team subscriptions',
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (userState.user?.uid !== this.userID) {
|
|
118
|
+
if (this.userID) {
|
|
119
|
+
this.unsubscribe('user id changed');
|
|
120
|
+
}
|
|
121
|
+
this.userID = userState.user?.uid;
|
|
122
|
+
}
|
|
123
|
+
this.currentUserSpaces = user?.spaces;
|
|
124
|
+
|
|
125
|
+
zipMapBriefsWithIDs(user.spaces).forEach(this.subscribeForUserSpaceChanges);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
public createSpace(
|
|
129
|
+
request: ICreateSpaceRequest,
|
|
130
|
+
): Observable<IRecord<ISpaceDbo>> {
|
|
131
|
+
return this.sneatApiService
|
|
132
|
+
.post<ICreateSpaceResponse>('spaces/create_space', request)
|
|
133
|
+
.pipe(map((response: ICreateSpaceResponse) => response.space));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// public getSpace(ref: ISpaceRef): Observable<ISpaceContext> {
|
|
137
|
+
// return this.watchSpace(ref).pipe(first());
|
|
138
|
+
// }
|
|
139
|
+
|
|
140
|
+
public watchSpace(id: string): Observable<ISpaceContext> {
|
|
141
|
+
if (!id) {
|
|
142
|
+
throw new Error('space id is a required parameter');
|
|
143
|
+
}
|
|
144
|
+
if (id === 'contacts') {
|
|
145
|
+
throw new Error('watchSpace({i}d===contacts})');
|
|
146
|
+
}
|
|
147
|
+
let subj = this.spaces$[id];
|
|
148
|
+
if (subj) {
|
|
149
|
+
return subj.asObservable().pipe(filter((s) => !!s));
|
|
150
|
+
}
|
|
151
|
+
let spaceContext: ISpaceContext | undefined = undefined;
|
|
152
|
+
if (this.currentUserSpaces) {
|
|
153
|
+
const userSpaceInfo = this.currentUserSpaces[id];
|
|
154
|
+
if (userSpaceInfo) {
|
|
155
|
+
spaceContext = {
|
|
156
|
+
id,
|
|
157
|
+
type: userSpaceInfo.type,
|
|
158
|
+
brief: spaceBriefFromUserSpaceInfo(userSpaceInfo),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
subj = new BehaviorSubject<ISpaceContext | undefined>(spaceContext);
|
|
163
|
+
this.spaces$[id] = subj;
|
|
164
|
+
if (this.userService.currentUserID) {
|
|
165
|
+
this.subscribeForSpaceChanges(id, subj);
|
|
166
|
+
} else {
|
|
167
|
+
this.userService.userState
|
|
168
|
+
.pipe(
|
|
169
|
+
filter((v) => v.status === AuthStatuses.authenticated),
|
|
170
|
+
first(),
|
|
171
|
+
)
|
|
172
|
+
.subscribe({
|
|
173
|
+
next: () => this.subscribeForSpaceChanges(id, subj),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return subj.asObservable().pipe(filter((s) => !!s));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
public onSpaceUpdated(space: ISpaceContext): void {
|
|
180
|
+
let team$ = this.spaces$[space.id];
|
|
181
|
+
if (team$) {
|
|
182
|
+
const prevTeam = team$.value;
|
|
183
|
+
space = { ...prevTeam, ...space };
|
|
184
|
+
} else {
|
|
185
|
+
this.spaces$[space.id] = team$ = new BehaviorSubject<
|
|
186
|
+
ISpaceContext | undefined
|
|
187
|
+
>(space);
|
|
188
|
+
}
|
|
189
|
+
team$.next(space);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public leaveSpace(request: ILeaveSpaceRequest): Observable<void> {
|
|
193
|
+
return this.sneatApiService.post('space/leave_space', request);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public updateRelated(request: IUpdateRelatedRequest): Observable<void> {
|
|
197
|
+
return this.sneatApiService.post('space/update_related', request);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// TODO: move to separate module
|
|
201
|
+
public deleteMetrics(space: string, metrics: string[]): Observable<void> {
|
|
202
|
+
return this.sneatApiService.post('space/remove_metrics', {
|
|
203
|
+
space,
|
|
204
|
+
metrics,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// TODO: move to separate module
|
|
209
|
+
public addMetric(space: string, metric: ISpaceMetric): Observable<void> {
|
|
210
|
+
if (!space) {
|
|
211
|
+
return throwError(() => 'space parameter is required');
|
|
212
|
+
}
|
|
213
|
+
const params = new HttpParams({ fromObject: { id: space } });
|
|
214
|
+
return this.sneatApiService.post('space/add_metric?' + params.toString(), {
|
|
215
|
+
metric,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private readonly subscribeForUserSpaceChanges = (
|
|
220
|
+
userSpaceBrief: IIdAndBrief<IUserSpaceBrief>,
|
|
221
|
+
): void => {
|
|
222
|
+
let subj = this.spaces$[userSpaceBrief.id];
|
|
223
|
+
if (subj) {
|
|
224
|
+
let space = subj.value;
|
|
225
|
+
if (space && !space?.type) {
|
|
226
|
+
space = {
|
|
227
|
+
...space,
|
|
228
|
+
type: userSpaceBrief.brief?.type,
|
|
229
|
+
brief: spaceBriefFromUserSpaceInfo(userSpaceBrief.brief),
|
|
230
|
+
};
|
|
231
|
+
subj.next(space);
|
|
232
|
+
}
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const space: ISpaceContext = {
|
|
237
|
+
id: userSpaceBrief.id,
|
|
238
|
+
type: userSpaceBrief.brief.type,
|
|
239
|
+
brief: spaceBriefFromUserSpaceInfo(userSpaceBrief.brief),
|
|
240
|
+
};
|
|
241
|
+
this.spaces$[space.id] = subj = new BehaviorSubject<
|
|
242
|
+
ISpaceContext | undefined
|
|
243
|
+
>(space);
|
|
244
|
+
this.subscribeForSpaceChanges(space.id, subj);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
private subscribeForSpaceChanges(
|
|
248
|
+
id: string,
|
|
249
|
+
subj: Subject<ISpaceContext | undefined>,
|
|
250
|
+
): void {
|
|
251
|
+
if (id === 'contacts') {
|
|
252
|
+
throw new Error('subscribeForSpaceChanges(id===contacts)');
|
|
253
|
+
}
|
|
254
|
+
const spacesCollection = runInInjectionContext(
|
|
255
|
+
this.injector,
|
|
256
|
+
() => collection(this.afs, 'spaces') as CollectionReference<ISpaceDbo>,
|
|
257
|
+
);
|
|
258
|
+
const o: Observable<ISpaceContext> = this.sfs
|
|
259
|
+
.watchByID(spacesCollection, id)
|
|
260
|
+
.pipe(
|
|
261
|
+
map((team) => {
|
|
262
|
+
// const _prevTeam = this.spaces$[id].value;
|
|
263
|
+
// if (prevTeam.assets) {
|
|
264
|
+
// team = { ...team, assets: prevTeam.assets};
|
|
265
|
+
// console.log('Reusing assets from prev context');
|
|
266
|
+
// }
|
|
267
|
+
return team;
|
|
268
|
+
}),
|
|
269
|
+
tap(() => {
|
|
270
|
+
// subj.next(team);
|
|
271
|
+
}),
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
this.subscriptions.push(
|
|
275
|
+
o.subscribe({
|
|
276
|
+
next: (v) => subj.next(v), // Do not use as "next: subj.next" because it will be called with wrong "this" context
|
|
277
|
+
error: (err) => subj.error(err),
|
|
278
|
+
}),
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private unsubscribe(_reason?: string): void {
|
|
283
|
+
try {
|
|
284
|
+
this.subscriptions.forEach((s) => s.unsubscribe());
|
|
285
|
+
this.subscriptions = [];
|
|
286
|
+
this.spaces$ = {};
|
|
287
|
+
} catch (e) {
|
|
288
|
+
this.errorLogger.logError(e, 'SpaceService failed to unsubscribe');
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.lib.base.json",
|
|
3
|
+
"exclude": [
|
|
4
|
+
"vite.config.ts",
|
|
5
|
+
"vite.config.mts",
|
|
6
|
+
"vitest.config.ts",
|
|
7
|
+
"vitest.config.mts",
|
|
8
|
+
"src/**/*.test.ts",
|
|
9
|
+
"src/**/*.spec.ts",
|
|
10
|
+
"src/**/*.test.tsx",
|
|
11
|
+
"src/**/*.spec.tsx",
|
|
12
|
+
"src/**/*.test.js",
|
|
13
|
+
"src/**/*.spec.js",
|
|
14
|
+
"src/**/*.test.jsx",
|
|
15
|
+
"src/**/*.spec.jsx",
|
|
16
|
+
"src/test-setup.ts",
|
|
17
|
+
"src/lib/testing/**/*"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../../dist/out-tsc",
|
|
5
|
+
"types": [
|
|
6
|
+
"vitest/globals",
|
|
7
|
+
"vitest/importMeta",
|
|
8
|
+
"vite/client",
|
|
9
|
+
"node",
|
|
10
|
+
"vitest"
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
"include": [
|
|
14
|
+
"vite.config.ts",
|
|
15
|
+
"vite.config.mts",
|
|
16
|
+
"vitest.config.ts",
|
|
17
|
+
"vitest.config.mts",
|
|
18
|
+
"src/**/*.test.ts",
|
|
19
|
+
"src/**/*.spec.ts",
|
|
20
|
+
"src/**/*.test.tsx",
|
|
21
|
+
"src/**/*.spec.tsx",
|
|
22
|
+
"src/**/*.test.js",
|
|
23
|
+
"src/**/*.spec.js",
|
|
24
|
+
"src/**/*.test.jsx",
|
|
25
|
+
"src/**/*.spec.jsx",
|
|
26
|
+
"src/**/*.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"files": [
|
|
29
|
+
"src/test-setup.ts"
|
|
30
|
+
]
|
|
31
|
+
}
|
package/vite.config.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types='vitest' />
|
|
2
|
+
import { defineConfig } from 'vitest/config';
|
|
3
|
+
import { createBaseViteConfig } from '../../../vite.config.base';
|
|
4
|
+
|
|
5
|
+
export default defineConfig(() =>
|
|
6
|
+
createBaseViteConfig({
|
|
7
|
+
dirname: __dirname,
|
|
8
|
+
name: 'space-services',
|
|
9
|
+
}),
|
|
10
|
+
);
|