bitwarden-estensione-browser/apps/web/src/app/settings/tax-info.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

171 lines
4.9 KiB
TypeScript
Raw Normal View History

2020-06-13 01:33:29 +02:00
import { Component, EventEmitter, Output } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
2022-02-24 12:10:07 +01:00
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { OrganizationTaxInfoUpdateRequest } from "@bitwarden/common/models/request/organizationTaxInfoUpdateRequest";
import { TaxInfoUpdateRequest } from "@bitwarden/common/models/request/taxInfoUpdateRequest";
import { TaxRateResponse } from "@bitwarden/common/models/response/taxRateResponse";
2020-06-13 01:33:29 +02:00
@Component({
selector: "app-tax-info",
templateUrl: "tax-info.component.html",
})
export class TaxInfoComponent {
@Output() onCountryChanged = new EventEmitter();
2022-02-24 12:10:07 +01:00
loading = true;
2020-06-13 01:33:29 +02:00
organizationId: string;
taxInfo: any = {
taxId: null,
line1: null,
line2: null,
city: null,
state: null,
postalCode: null,
country: "US",
includeTaxId: false,
};
taxRates: TaxRateResponse[];
2020-06-13 01:33:29 +02:00
private pristine: any = {
taxId: null,
line1: null,
line2: null,
city: null,
state: null,
postalCode: null,
country: "US",
includeTaxId: false,
};
constructor(
private apiService: ApiService,
private route: ActivatedRoute,
private logService: LogService
) {}
2020-06-13 01:33:29 +02:00
async ngOnInit() {
this.route.parent.parent.params.subscribe(async (params) => {
2020-06-13 01:33:29 +02:00
this.organizationId = params.organizationId;
if (this.organizationId) {
try {
const taxInfo = await this.apiService.getOrganizationTaxInfo(this.organizationId);
if (taxInfo) {
this.taxInfo.taxId = taxInfo.taxId;
this.taxInfo.state = taxInfo.state;
this.taxInfo.line1 = taxInfo.line1;
this.taxInfo.line2 = taxInfo.line2;
this.taxInfo.city = taxInfo.city;
this.taxInfo.state = taxInfo.state;
this.taxInfo.postalCode = taxInfo.postalCode;
this.taxInfo.country = taxInfo.country || "US";
this.taxInfo.includeTaxId =
this.taxInfo.country !== "US" &&
(!!taxInfo.taxId ||
!!taxInfo.line1 ||
!!taxInfo.line2 ||
!!taxInfo.city ||
!!taxInfo.state);
}
} catch (e) {
this.logService.error(e);
}
2020-06-13 01:33:29 +02:00
} else {
try {
const taxInfo = await this.apiService.getTaxInfo();
if (taxInfo) {
this.taxInfo.postalCode = taxInfo.postalCode;
this.taxInfo.country = taxInfo.country || "US";
}
} catch (e) {
this.logService.error(e);
2020-06-13 01:33:29 +02:00
}
}
this.pristine = Object.assign({}, this.taxInfo);
// If not the default (US) then trigger onCountryChanged
if (this.taxInfo.country !== "US") {
this.onCountryChanged.emit();
}
});
try {
const taxRates = await this.apiService.getTaxRates();
if (taxRates) {
this.taxRates = taxRates.data;
}
} catch (e) {
this.logService.error(e);
} finally {
this.loading = false;
}
2020-06-13 01:33:29 +02:00
}
get taxRate() {
if (this.taxRates != null) {
const localTaxRate = this.taxRates.find(
(x) => x.country === this.taxInfo.country && x.postalCode === this.taxInfo.postalCode
);
return localTaxRate?.rate ?? null;
}
2021-12-17 15:57:11 +01:00
}
2020-06-13 01:33:29 +02:00
getTaxInfoRequest(): TaxInfoUpdateRequest {
if (this.organizationId) {
const request = new OrganizationTaxInfoUpdateRequest();
request.taxId = this.taxInfo.taxId;
request.state = this.taxInfo.state;
request.line1 = this.taxInfo.line1;
request.line2 = this.taxInfo.line2;
request.city = this.taxInfo.city;
request.state = this.taxInfo.state;
request.postalCode = this.taxInfo.postalCode;
request.country = this.taxInfo.country;
return request;
} else {
const request = new TaxInfoUpdateRequest();
request.postalCode = this.taxInfo.postalCode;
request.country = this.taxInfo.country;
return request;
}
2021-12-17 15:57:11 +01:00
}
2020-06-13 01:33:29 +02:00
submitTaxInfo(): Promise<any> {
if (!this.hasChanged()) {
return new Promise<void>((resolve) => {
resolve();
});
2020-06-13 01:33:29 +02:00
}
const request = this.getTaxInfoRequest();
return this.organizationId
? this.apiService.putOrganizationTaxInfo(
this.organizationId,
request as OrganizationTaxInfoUpdateRequest
2021-12-17 15:57:11 +01:00
)
2020-06-13 01:33:29 +02:00
: this.apiService.putTaxInfo(request);
2021-12-17 15:57:11 +01:00
}
2020-06-13 01:33:29 +02:00
changeCountry() {
if (this.taxInfo.country === "US") {
this.taxInfo.includeTaxId = false;
this.taxInfo.taxId = null;
this.taxInfo.line1 = null;
this.taxInfo.line2 = null;
this.taxInfo.city = null;
this.taxInfo.state = null;
}
this.onCountryChanged.emit();
2021-12-17 15:57:11 +01:00
}
2020-06-13 01:33:29 +02:00
private hasChanged(): boolean {
for (const key in this.taxInfo) {
2022-02-24 12:10:07 +01:00
// eslint-disable-next-line
2020-06-13 01:33:29 +02:00
if (this.pristine.hasOwnProperty(key) && this.pristine[key] !== this.taxInfo[key]) {
return true;
}
}
return false;
2021-12-17 15:57:11 +01:00
}
2020-06-13 01:33:29 +02:00
}