Skip to content

Commit 3a7cfaf

Browse files
retrieving tenants
Signed-off-by: jackdisalvatore <[email protected]>
1 parent db5ca57 commit 3a7cfaf

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

frontend/src/lib/context/UnityAuthContext.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '$lib/services/ServerErrors/ServerErrors';
1212
import { isAxiosError } from 'axios';
1313
import type { CompleteLoginResponse, UnityAuthServiceProps } from '$lib/services/UnityAuth/shared';
14+
import { TenantsResolverImpl } from '$lib/services/TenantsResolver/TenantsResolver';
1415

1516
const unityAuthCtxKey = Symbol();
1617

@@ -24,12 +25,17 @@ export type UnityAuthContext = {
2425
} & UnityAuthAlert;
2526

2627
export type UnityAuthContextProviderProps = {
27-
unityAuthServiceProps: Omit<UnityAuthServiceProps, 'userPermissionsResolver'>;
28+
unityAuthServiceProps: Omit<UnityAuthServiceProps, 'tenantsResolver'>;
2829
mode: Mode;
2930
};
3031

3132
export function createUnityAuthContext(props: UnityAuthContextProviderProps & UnityAuthAlert) {
33+
const tenantsResolver = new TenantsResolverImpl({
34+
libreBaseUrl: props.unityAuthServiceProps.baseURL
35+
});
36+
3237
const unityAuthService = unityAuthServiceFactory({
38+
tenantsResolver,
3339
...props.unityAuthServiceProps
3440
});
3541

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import axios from 'axios';
2+
import type { UnityAuthLoginResponse } from '../UnityAuth/shared';
3+
import { TenantsResponseSchema, type TenantsResponse } from './shared';
4+
5+
export type TenantsResolver = {
6+
getTenants(loginRes: UnityAuthLoginResponse): Promise<TenantsResponse>;
7+
};
8+
9+
export class TenantsResolverImpl implements TenantsResolver {
10+
constructor(private props: { libreBaseUrl: string }) {}
11+
12+
async getTenants(loginRes: UnityAuthLoginResponse): Promise<TenantsResponse> {
13+
const res = await axios.get<unknown>(this.props.libreBaseUrl + `/api/tenants`, {
14+
headers: {
15+
Authorization: `Bearer ${loginRes.access_token}`
16+
}
17+
});
18+
return TenantsResponseSchema.parse(res.data);
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { z } from 'zod';
2+
3+
export const TenantsSchema = z.object({
4+
id: z.number(),
5+
name: z.string()
6+
});
7+
8+
export type LibrePermissions = z.infer<typeof TenantsSchema>;
9+
10+
export const TenantsSuccessResponseSchema = z.array(TenantsSchema);
11+
12+
export type TenantsSuccessResponse = z.infer<typeof TenantsSuccessResponseSchema>;
13+
14+
export function isTenantsSuccessResponse(unknown: unknown): unknown is TenantsSuccessResponse {
15+
return TenantsSuccessResponseSchema.safeParse(unknown).success;
16+
}
17+
18+
export const TenantsFailureResponseSchema = z.object({
19+
errorMessage: z.string()
20+
});
21+
22+
export type TenantsFailureResponse = z.infer<typeof TenantsFailureResponseSchema>;
23+
24+
export const TenantsResponseSchema = z.union([
25+
TenantsSuccessResponseSchema,
26+
TenantsFailureResponseSchema
27+
]);
28+
29+
export type TenantsResponse = z.infer<typeof TenantsResponseSchema>;

frontend/src/lib/services/UnityAuth/UnityAuth.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
} from './shared';
99
import axios from 'axios';
1010
import { browser } from '$app/environment';
11+
import { isTenantsSuccessResponse } from '../TenantsResolver/shared';
12+
import type { TenantsResolver } from '../TenantsResolver/TenantsResolver';
1113

1214
export type UnityAuthEventMap = {
1315
login: CompleteLoginResponse;
@@ -26,6 +28,7 @@ export class UnityAuthServiceImpl
2628
{
2729
private loginDataKey: string = 'loginData';
2830
private axiosInstance: AxiosInstance;
31+
private tenantsResolver: TenantsResolver;
2932
private loginData: CompleteLoginResponse | undefined;
3033
private constructor(props: UnityAuthServiceProps) {
3134
super();
@@ -34,6 +37,8 @@ export class UnityAuthServiceImpl
3437

3538
this.loginData = this.retrieveLoginData();
3639
this.publish('logout', undefined);
40+
41+
this.tenantsResolver = props.tenantsResolver;
3742
}
3843

3944
public static create(props: UnityAuthServiceProps) {
@@ -51,6 +56,14 @@ export class UnityAuthServiceImpl
5156
});
5257

5358
const loginRes = UnityAuthLoginResponseSchema.parse(res.data);
59+
60+
const tenantsRes = await this.tenantsResolver.getTenants(loginRes);
61+
62+
if (!isTenantsSuccessResponse(tenantsRes)) {
63+
throw new Error(tenantsRes.errorMessage);
64+
}
65+
console.log(tenantsRes);
66+
5467
const completeLoginRes: CompleteLoginResponse = { ...loginRes };
5568

5669
this.loginData = completeLoginRes;

frontend/src/lib/services/UnityAuth/shared.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { z } from 'zod';
2+
import type { TenantsResolver } from '../TenantsResolver/TenantsResolver';
23

34
export const UnityAuthServicePropsSchema = z.object({
45
baseURL: z.string()
56
});
67

7-
export type UnityAuthServiceProps = z.infer<typeof UnityAuthServicePropsSchema>;
8+
export type UnityAuthServiceProps = z.infer<typeof UnityAuthServicePropsSchema> & {
9+
tenantsResolver: TenantsResolver;
10+
};
811

912
export const UnityAuthLoginResponseSchema = z.object({
1013
access_token: z.string(),

0 commit comments

Comments
 (0)