Multiple bug fixes

This commit is contained in:
Igor Kulikov 2022-07-06 18:54:34 +03:00
parent a22e9d580f
commit a0c83317d3
5 changed files with 48 additions and 32 deletions

View File

@ -261,7 +261,7 @@ public class DashboardController extends BaseController {
checkParameter(DASHBOARD_ID, strDashboardId); checkParameter(DASHBOARD_ID, strDashboardId);
DashboardId dashboardId = new DashboardId(toUUID(strDashboardId)); DashboardId dashboardId = new DashboardId(toUUID(strDashboardId));
Dashboard dashboard = checkDashboardId(dashboardId, Operation.ASSIGN_TO_CUSTOMER); Dashboard dashboard = checkDashboardId(dashboardId, Operation.ASSIGN_TO_CUSTOMER);
Set<CustomerId> customerIds = customerIdFromStr(strCustomerIds, dashboard); Set<CustomerId> customerIds = customerIdFromStr(strCustomerIds);
return tbDashboardService.updateDashboardCustomers(dashboard, customerIds, getCurrentUser()); return tbDashboardService.updateDashboardCustomers(dashboard, customerIds, getCurrentUser());
} }
@ -281,7 +281,7 @@ public class DashboardController extends BaseController {
checkParameter(DASHBOARD_ID, strDashboardId); checkParameter(DASHBOARD_ID, strDashboardId);
DashboardId dashboardId = new DashboardId(toUUID(strDashboardId)); DashboardId dashboardId = new DashboardId(toUUID(strDashboardId));
Dashboard dashboard = checkDashboardId(dashboardId, Operation.ASSIGN_TO_CUSTOMER); Dashboard dashboard = checkDashboardId(dashboardId, Operation.ASSIGN_TO_CUSTOMER);
Set<CustomerId> customerIds = customerIdFromStr(strCustomerIds, dashboard); Set<CustomerId> customerIds = customerIdFromStr(strCustomerIds);
return tbDashboardService.addDashboardCustomers(dashboard, customerIds, getCurrentUser()); return tbDashboardService.addDashboardCustomers(dashboard, customerIds, getCurrentUser());
} }
@ -301,7 +301,7 @@ public class DashboardController extends BaseController {
checkParameter(DASHBOARD_ID, strDashboardId); checkParameter(DASHBOARD_ID, strDashboardId);
DashboardId dashboardId = new DashboardId(toUUID(strDashboardId)); DashboardId dashboardId = new DashboardId(toUUID(strDashboardId));
Dashboard dashboard = checkDashboardId(dashboardId, Operation.UNASSIGN_FROM_CUSTOMER); Dashboard dashboard = checkDashboardId(dashboardId, Operation.UNASSIGN_FROM_CUSTOMER);
Set<CustomerId> customerIds = customerIdFromStr(strCustomerIds, dashboard); Set<CustomerId> customerIds = customerIdFromStr(strCustomerIds);
return tbDashboardService.removeDashboardCustomers(dashboard, customerIds, getCurrentUser()); return tbDashboardService.removeDashboardCustomers(dashboard, customerIds, getCurrentUser());
} }
@ -704,14 +704,11 @@ public class DashboardController extends BaseController {
} }
} }
private Set<CustomerId> customerIdFromStr(String[] strCustomerIds, Dashboard dashboard) { private Set<CustomerId> customerIdFromStr(String[] strCustomerIds) {
Set<CustomerId> customerIds = new HashSet<>(); Set<CustomerId> customerIds = new HashSet<>();
if (strCustomerIds != null) { if (strCustomerIds != null) {
for (String strCustomerId : strCustomerIds) { for (String strCustomerId : strCustomerIds) {
CustomerId customerId = new CustomerId(UUID.fromString(strCustomerId)); customerIds.add(new CustomerId(UUID.fromString(strCustomerId)));
if (dashboard.isAssignedToCustomer(customerId)) {
customerIds.add(customerId);
}
} }
} }
return customerIds; return customerIds;

View File

@ -183,11 +183,17 @@ public class DefaultTbDashboardService extends AbstractTbEntityService implement
TenantId tenantId = dashboard.getTenantId(); TenantId tenantId = dashboard.getTenantId();
DashboardId dashboardId = dashboard.getId(); DashboardId dashboardId = dashboard.getId();
try { try {
if (customerIds.isEmpty()) { Set<CustomerId> addedCustomerIds = new HashSet<>();
for (CustomerId customerId : customerIds) {
if (!dashboard.isAssignedToCustomer(customerId)) {
addedCustomerIds.add(customerId);
}
}
if (addedCustomerIds.isEmpty()) {
return dashboard; return dashboard;
} else { } else {
Dashboard savedDashboard = null; Dashboard savedDashboard = null;
for (CustomerId customerId : customerIds) { for (CustomerId customerId : addedCustomerIds) {
savedDashboard = checkNotNull(dashboardService.assignDashboardToCustomer(tenantId, dashboardId, customerId)); savedDashboard = checkNotNull(dashboardService.assignDashboardToCustomer(tenantId, dashboardId, customerId));
ShortCustomerInfo customerInfo = savedDashboard.getAssignedCustomerInfo(customerId); ShortCustomerInfo customerInfo = savedDashboard.getAssignedCustomerInfo(customerId);
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, dashboardId, customerId, savedDashboard, notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, dashboardId, customerId, savedDashboard,
@ -207,11 +213,17 @@ public class DefaultTbDashboardService extends AbstractTbEntityService implement
TenantId tenantId = dashboard.getTenantId(); TenantId tenantId = dashboard.getTenantId();
DashboardId dashboardId = dashboard.getId(); DashboardId dashboardId = dashboard.getId();
try { try {
if (customerIds.isEmpty()) { Set<CustomerId> removedCustomerIds = new HashSet<>();
for (CustomerId customerId : customerIds) {
if (dashboard.isAssignedToCustomer(customerId)) {
removedCustomerIds.add(customerId);
}
}
if (removedCustomerIds.isEmpty()) {
return dashboard; return dashboard;
} else { } else {
Dashboard savedDashboard = null; Dashboard savedDashboard = null;
for (CustomerId customerId : customerIds) { for (CustomerId customerId : removedCustomerIds) {
ShortCustomerInfo customerInfo = dashboard.getAssignedCustomerInfo(customerId); ShortCustomerInfo customerInfo = dashboard.getAssignedCustomerInfo(customerId);
savedDashboard = checkNotNull(dashboardService.unassignDashboardFromCustomer(tenantId, dashboardId, customerId)); savedDashboard = checkNotNull(dashboardService.unassignDashboardFromCustomer(tenantId, dashboardId, customerId));
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, dashboardId, customerId, savedDashboard, notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, dashboardId, customerId, savedDashboard,

View File

@ -543,6 +543,8 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
ctx.addUuidParameter("permissions_customer_id", ctx.getCustomerId().getId()); ctx.addUuidParameter("permissions_customer_id", ctx.getCustomerId().getId());
if (ctx.getEntityType() == EntityType.CUSTOMER) { if (ctx.getEntityType() == EntityType.CUSTOMER) {
return "e.tenant_id=:permissions_tenant_id and e.id=:permissions_customer_id"; return "e.tenant_id=:permissions_tenant_id and e.id=:permissions_customer_id";
} else if (ctx.getEntityType() == EntityType.API_USAGE_STATE) {
return "e.tenant_id=:permissions_tenant_id and e.entity_id=:permissions_customer_id";
} else { } else {
return "e.tenant_id=:permissions_tenant_id and e.customer_id=:permissions_customer_id"; return "e.tenant_id=:permissions_tenant_id and e.customer_id=:permissions_customer_id";
} }

View File

@ -35,7 +35,6 @@
type="tel" type="tel"
matInput matInput
placeholder="{{ placeholder | translate }}" placeholder="{{ placeholder | translate }}"
[pattern]="phoneNumberPattern"
(focus)="focus()" (focus)="focus()"
autocomplete="off" autocomplete="off"
[required]="required"> [required]="required">

View File

@ -30,7 +30,6 @@ import {
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { Country, CountryData } from '@shared/models/country.models'; import { Country, CountryData } from '@shared/models/country.models';
import examples from 'libphonenumber-js/examples.mobile.json'; import examples from 'libphonenumber-js/examples.mobile.json';
import { phoneNumberPattern } from '@shared/models/settings.models';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { FloatLabelType, MatFormFieldAppearance } from '@angular/material/form-field/form-field'; import { FloatLabelType, MatFormFieldAppearance } from '@angular/material/form-field/form-field';
@ -82,7 +81,6 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
phonePlaceholder = '+12015550123'; phonePlaceholder = '+12015550123';
flagIcon: string; flagIcon: string;
phoneFormGroup: FormGroup; phoneFormGroup: FormGroup;
phoneNumberPattern = phoneNumberPattern;
private isLoading = true; private isLoading = true;
get isLoad(): boolean { get isLoad(): boolean {
@ -103,7 +101,8 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
private baseCode = 127397; private baseCode = 127397;
private countryCallingCode = '+'; private countryCallingCode = '+';
private modelValue: string; private modelValue: string;
private valueChange$: Subscription = null; private changeSubscriptions: Subscription[] = [];
private propagateChange = (v: any) => { }; private propagateChange = (v: any) => { };
constructor(private translate: TranslateService, constructor(private translate: TranslateService,
@ -116,7 +115,7 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
} }
ngOnInit(): void { ngOnInit(): void {
const validators: ValidatorFn[] = [Validators.pattern(phoneNumberPattern), this.validatePhoneNumber()]; const validators: ValidatorFn[] = [(c: FormControl) => Validators.pattern(this.getPhoneNumberPattern())(c), this.validatePhoneNumber()];
if (this.required) { if (this.required) {
validators.push(Validators.required); validators.push(Validators.required);
} }
@ -125,12 +124,12 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
phoneNumber: [null, validators] phoneNumber: [null, validators]
}); });
this.valueChange$ = this.phoneFormGroup.get('phoneNumber').valueChanges.subscribe(value => { this.changeSubscriptions.push(this.phoneFormGroup.get('phoneNumber').valueChanges.subscribe(value => {
this.updateModel(); this.updateModel();
this.defineCountryFromNumber(value); this.defineCountryFromNumber(value);
}); }));
this.phoneFormGroup.get('country').valueChanges.subscribe(value => { this.changeSubscriptions.push(this.phoneFormGroup.get('country').valueChanges.subscribe(value => {
if (value) { if (value) {
const code = this.countryCallingCode; const code = this.countryCallingCode;
this.getFlagAndPhoneNumberData(value); this.getFlagAndPhoneNumberData(value);
@ -142,21 +141,23 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
} }
} }
} }
}); }));
} }
ngOnDestroy() { ngOnDestroy() {
if (this.valueChange$) { for (const subscription of this.changeSubscriptions) {
this.valueChange$.unsubscribe(); subscription.unsubscribe();
} }
} }
focus() { focus() {
const phoneNumber = this.phoneFormGroup.get('phoneNumber'); const phoneNumber = this.phoneFormGroup.get('phoneNumber');
this.phoneFormGroup.markAsPristine();
this.phoneFormGroup.markAsUntouched();
if (!phoneNumber.value) { if (!phoneNumber.value) {
phoneNumber.patchValue(this.countryCallingCode); phoneNumber.patchValue(this.countryCallingCode, {emitEvent: false});
}
if (phoneNumber.untouched && this.countryCallingCode !== phoneNumber.value) {
phoneNumber.markAsTouched();
phoneNumber.updateValueAndValidity();
} }
} }
@ -182,7 +183,7 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
validatePhoneNumber(): ValidatorFn { validatePhoneNumber(): ValidatorFn {
return (c: FormControl) => { return (c: FormControl) => {
const phoneNumber = c.value; const phoneNumber = c.value;
if (phoneNumber && this.parsePhoneNumberFromString) { if (phoneNumber && this.countryCallingCode !== phoneNumber && this.parsePhoneNumberFromString) {
const parsedPhoneNumber = this.parsePhoneNumberFromString(phoneNumber); const parsedPhoneNumber = this.parsePhoneNumberFromString(phoneNumber);
if (!parsedPhoneNumber?.isValid() || !parsedPhoneNumber?.isPossible()) { if (!parsedPhoneNumber?.isValid() || !parsedPhoneNumber?.isPossible()) {
return { return {
@ -206,8 +207,13 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
} }
} }
private getPhoneNumberPattern(): RegExp {
return new RegExp(`^${this.countryCallingCode.replace('+', '\\+')}$|^\\+[1-9]\\d{1,14}$`);
}
validate(): ValidationErrors | null { validate(): ValidationErrors | null {
return this.phoneFormGroup.get('phoneNumber').valid ? null : { const phoneNumber = this.phoneFormGroup.get('phoneNumber');
return phoneNumber.valid || phoneNumber.untouched || this.countryCallingCode === phoneNumber.value ? null : {
phoneFormGroup: false phoneFormGroup: false
}; };
} }
@ -232,18 +238,18 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
this.modelValue = phoneNumber; this.modelValue = phoneNumber;
let country = this.defaultCountry; let country = this.defaultCountry;
if (this.parsePhoneNumberFromString) { if (this.parsePhoneNumberFromString) {
country = phoneNumber ? this.parsePhoneNumberFromString(phoneNumber)?.country : this.defaultCountry; country = phoneNumber ? this.parsePhoneNumberFromString(phoneNumber)?.country || this.defaultCountry : this.defaultCountry;
this.getFlagAndPhoneNumberData(country); this.getFlagAndPhoneNumberData(country);
} }
this.phoneFormGroup.patchValue({phoneNumber, country}, {emitEvent: !phoneNumber}); this.phoneFormGroup.reset({phoneNumber, country}, {emitEvent: false});
} }
private updateModel() { private updateModel() {
const phoneNumber = this.phoneFormGroup.get('phoneNumber'); const phoneNumber = this.phoneFormGroup.get('phoneNumber');
if (phoneNumber.valid && phoneNumber.value) { if (phoneNumber.valid) {
this.modelValue = phoneNumber.value; this.modelValue = phoneNumber.value;
this.propagateChange(this.modelValue); this.propagateChange(this.modelValue);
} else if (phoneNumber.invalid && phoneNumber.value) { } else {
this.propagateChange(null); this.propagateChange(null);
} }
} }