2022-05-16 03:58:36 +02:00
|
|
|
import { AuthService } from "jslib-common/abstractions/auth.service";
|
2021-12-20 18:04:00 +01:00
|
|
|
import { EnvironmentService } from "jslib-common/abstractions/environment.service";
|
2021-12-28 21:38:51 +01:00
|
|
|
import { StateService } from "jslib-common/abstractions/state.service";
|
2021-12-20 18:04:00 +01:00
|
|
|
import { SyncService } from "jslib-common/abstractions/sync.service";
|
2022-05-16 03:58:36 +02:00
|
|
|
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
|
2021-12-20 18:04:00 +01:00
|
|
|
import { Response } from "jslib-node/cli/models/response";
|
2020-06-17 17:01:34 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
import { TemplateResponse } from "../models/response/templateResponse";
|
2020-06-17 02:43:30 +02:00
|
|
|
|
|
|
|
export class StatusCommand {
|
2021-12-20 18:04:00 +01:00
|
|
|
constructor(
|
|
|
|
private envService: EnvironmentService,
|
|
|
|
private syncService: SyncService,
|
2021-12-28 21:38:51 +01:00
|
|
|
private stateService: StateService,
|
2022-05-16 03:58:36 +02:00
|
|
|
private authService: AuthService
|
2021-12-20 18:04:00 +01:00
|
|
|
) {}
|
|
|
|
|
|
|
|
async run(): Promise<Response> {
|
|
|
|
try {
|
|
|
|
const baseUrl = this.baseUrl();
|
|
|
|
const status = await this.status();
|
|
|
|
const lastSync = await this.syncService.getLastSync();
|
2021-12-28 21:38:51 +01:00
|
|
|
const userId = await this.stateService.getUserId();
|
|
|
|
const email = await this.stateService.getEmail();
|
2021-12-20 18:04:00 +01:00
|
|
|
|
|
|
|
return Response.success(
|
|
|
|
new TemplateResponse({
|
|
|
|
serverUrl: baseUrl,
|
|
|
|
lastSync: lastSync,
|
|
|
|
userEmail: email,
|
|
|
|
userId: userId,
|
|
|
|
status: status,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
2020-06-17 02:43:30 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2020-06-17 02:43:30 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
private baseUrl(): string {
|
|
|
|
return this.envService.getUrls().base;
|
|
|
|
}
|
2020-06-17 02:43:30 +02:00
|
|
|
|
2022-05-16 03:58:36 +02:00
|
|
|
private async status(): Promise<"unauthenticated" | "locked" | "unlocked"> {
|
|
|
|
const authStatus = await this.authService.getAuthStatus();
|
|
|
|
if (authStatus === AuthenticationStatus.Unlocked) {
|
|
|
|
return "unlocked";
|
|
|
|
} else if (authStatus === AuthenticationStatus.Locked) {
|
|
|
|
return "locked";
|
|
|
|
} else {
|
2021-12-20 18:04:00 +01:00
|
|
|
return "unauthenticated";
|
2020-06-17 02:43:30 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2020-06-17 02:43:30 +02:00
|
|
|
}
|