2021-11-22 14:41:40 +01:00
|
|
|
import { Component, OnInit, ViewChild, ViewContainerRef } from "@angular/core";
|
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
|
|
import { first } from "rxjs/operators";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ModalService } from "@bitwarden/angular/services/modal.service";
|
|
|
|
import { ValidationService } from "@bitwarden/angular/services/validation.service";
|
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { OrganizationService } from "@bitwarden/common/abstractions/organization.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { SyncService } from "@bitwarden/common/abstractions/sync.service";
|
|
|
|
import { PlanSponsorshipType } from "@bitwarden/common/enums/planSponsorshipType";
|
|
|
|
import { PlanType } from "@bitwarden/common/enums/planType";
|
|
|
|
import { ProductType } from "@bitwarden/common/enums/productType";
|
|
|
|
import { Organization } from "@bitwarden/common/models/domain/organization";
|
|
|
|
import { OrganizationSponsorshipRedeemRequest } from "@bitwarden/common/models/request/organization/organizationSponsorshipRedeemRequest";
|
2021-11-22 14:41:40 +01:00
|
|
|
|
2022-08-03 21:40:04 +02:00
|
|
|
import { OrganizationPlansComponent } from "../../settings/organization-plans.component";
|
|
|
|
import { DeleteOrganizationComponent } from "../settings/delete-organization.component";
|
2021-11-22 14:41:40 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "families-for-enterprise-setup",
|
|
|
|
templateUrl: "families-for-enterprise-setup.component.html",
|
|
|
|
})
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2021-11-22 14:41:40 +01:00
|
|
|
export class FamiliesForEnterpriseSetupComponent implements OnInit {
|
2021-11-24 15:33:34 +01:00
|
|
|
@ViewChild(OrganizationPlansComponent, { static: false })
|
2021-11-22 14:41:40 +01:00
|
|
|
set organizationPlansComponent(value: OrganizationPlansComponent) {
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
value.plan = PlanType.FamiliesAnnually;
|
|
|
|
value.product = ProductType.Families;
|
|
|
|
value.acceptingSponsorship = true;
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2021-11-22 14:41:40 +01:00
|
|
|
value.onSuccess.subscribe(this.onOrganizationCreateSuccess.bind(this));
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
@ViewChild("deleteOrganizationTemplate", { read: ViewContainerRef, static: true })
|
|
|
|
deleteModalRef: ViewContainerRef;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
loading = true;
|
2021-11-24 21:31:16 +01:00
|
|
|
badToken = false;
|
2021-11-22 14:41:40 +01:00
|
|
|
formPromise: Promise<any>;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
token: string;
|
|
|
|
existingFamilyOrganizations: Organization[];
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2022-02-24 12:10:07 +01:00
|
|
|
showNewOrganization = false;
|
2021-11-22 14:41:40 +01:00
|
|
|
_organizationPlansComponent: OrganizationPlansComponent;
|
2022-02-24 12:10:07 +01:00
|
|
|
_selectedFamilyOrganizationId = "";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-12-07 20:41:45 +01:00
|
|
|
constructor(
|
|
|
|
private router: Router,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
2021-11-22 14:41:40 +01:00
|
|
|
private i18nService: I18nService,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private apiService: ApiService,
|
|
|
|
private syncService: SyncService,
|
2021-12-14 17:10:26 +01:00
|
|
|
private validationService: ValidationService,
|
|
|
|
private organizationService: OrganizationService,
|
2021-11-22 14:41:40 +01:00
|
|
|
private modalService: ModalService
|
|
|
|
) {}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
async ngOnInit() {
|
|
|
|
document.body.classList.remove("layout_frontend");
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe
|
2021-11-22 14:41:40 +01:00
|
|
|
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
|
|
|
|
const error = qParams.token == null;
|
|
|
|
if (error) {
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"error",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("sponsoredFamiliesAcceptFailed"),
|
|
|
|
{ timeout: 10000 }
|
|
|
|
);
|
2021-11-22 14:41:40 +01:00
|
|
|
this.router.navigate(["/"]);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
this.token = qParams.token;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
await this.syncService.fullSync(true);
|
|
|
|
this.badToken = !(await this.apiService.postPreValidateSponsorshipToken(this.token));
|
|
|
|
this.loading = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
this.existingFamilyOrganizations = (await this.organizationService.getAll()).filter(
|
|
|
|
(o) => o.planProductType === ProductType.Families
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
if (this.existingFamilyOrganizations.length === 0) {
|
|
|
|
this.selectedFamilyOrganizationId = "createNew";
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
});
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
async submit() {
|
|
|
|
this.formPromise = this.doSubmit(this._selectedFamilyOrganizationId);
|
|
|
|
await this.formPromise;
|
|
|
|
this.formPromise = null;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
get selectedFamilyOrganizationId() {
|
|
|
|
return this._selectedFamilyOrganizationId;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
set selectedFamilyOrganizationId(value: string) {
|
2021-11-24 21:31:16 +01:00
|
|
|
this._selectedFamilyOrganizationId = value;
|
|
|
|
this.showNewOrganization = value === "createNew";
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
private async doSubmit(organizationId: string) {
|
2021-12-17 15:57:11 +01:00
|
|
|
try {
|
2021-11-22 14:41:40 +01:00
|
|
|
const request = new OrganizationSponsorshipRedeemRequest();
|
2021-11-24 21:31:16 +01:00
|
|
|
request.planSponsorshipType = PlanSponsorshipType.FamiliesForEnterprise;
|
2021-11-22 14:41:40 +01:00
|
|
|
request.sponsoredOrganizationId = organizationId;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-24 21:31:16 +01:00
|
|
|
await this.apiService.postRedeemSponsorship(this.token, request);
|
2021-11-22 14:41:40 +01:00
|
|
|
this.platformUtilsService.showToast(
|
2021-12-17 15:57:11 +01:00
|
|
|
"success",
|
|
|
|
null,
|
2021-12-14 17:10:26 +01:00
|
|
|
this.i18nService.t("sponsoredFamiliesOfferRedeemed")
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2021-12-14 17:10:26 +01:00
|
|
|
await this.syncService.fullSync(true);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-11-22 14:41:40 +01:00
|
|
|
this.router.navigate(["/"]);
|
2021-12-14 17:10:26 +01:00
|
|
|
} catch (e) {
|
2021-11-22 14:41:40 +01:00
|
|
|
if (this.showNewOrganization) {
|
|
|
|
await this.modalService.openViewRef(
|
2021-12-14 17:10:26 +01:00
|
|
|
DeleteOrganizationComponent,
|
2021-11-22 14:41:40 +01:00
|
|
|
this.deleteModalRef,
|
2021-12-17 15:57:11 +01:00
|
|
|
(comp) => {
|
2021-12-14 17:10:26 +01:00
|
|
|
comp.organizationId = organizationId;
|
2022-03-04 15:03:48 +01:00
|
|
|
comp.deleteOrganizationRequestType = "InvalidFamiliesForEnterprise";
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2021-12-14 17:10:26 +01:00
|
|
|
comp.onSuccess.subscribe(() => {
|
2021-11-22 14:41:40 +01:00
|
|
|
this.router.navigate(["/"]);
|
2021-12-17 15:57:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-12-14 17:10:26 +01:00
|
|
|
this.validationService.showError(this.i18nService.t("sponsorshipTokenHasExpired"));
|
2021-11-22 14:41:40 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
|
|
|
|
private async onOrganizationCreateSuccess(value: any) {
|
|
|
|
// Use newly created organization id
|
|
|
|
await this.doSubmit(value.organizationId);
|
|
|
|
}
|
|
|
|
}
|