keep plan selection during keycloak hop

This commit is contained in:
2024-09-17 16:51:30 +02:00
parent c00c2caccc
commit eaa8a5064f
9 changed files with 108 additions and 42 deletions

View File

@@ -7,6 +7,7 @@ import { switchMap } from 'rxjs';
import { User } from '../../../../../bizmatch-server/src/models/db.model';
import { Checkout, KeycloakUser } from '../../../../../bizmatch-server/src/models/main.model';
import { environment } from '../../../environments/environment';
import { AuditService } from '../../services/audit.service';
import { UserService } from '../../services/user.service';
import { SharedModule } from '../../shared/shared/shared.module';
import { map2User } from '../../utils/utils';
@@ -24,25 +25,50 @@ export class PricingComponent {
pricingOverview: boolean | undefined = this.activatedRoute.snapshot.data['pricingOverview'] as boolean | undefined;
keycloakUser: KeycloakUser;
user: User;
constructor(public keycloakService: KeycloakService, private http: HttpClient, private stripeService: StripeService, private activatedRoute: ActivatedRoute, private userService: UserService, private router: Router) {}
constructor(
public keycloakService: KeycloakService,
private http: HttpClient,
private stripeService: StripeService,
private activatedRoute: ActivatedRoute,
private userService: UserService,
private router: Router,
private auditService: AuditService,
) {}
async ngOnInit() {
const token = await this.keycloakService.getToken();
this.keycloakUser = map2User(token);
if (this.keycloakUser) {
if (this.id === 'free') {
this.user = await this.userService.getByMail(this.keycloakUser.email);
this.user.subscriptionPlan = 'free';
await this.userService.saveGuaranteed(this.user);
this.router.navigate([`/account`]);
} else if (this.id) {
this.checkout({ priceId: atob(this.id), email: this.keycloakUser.email, name: `${this.keycloakUser.firstName} ${this.keycloakUser.lastName}` });
} else if (!this.id && !this.pricingOverview) {
this.user = await this.userService.getByMail(this.keycloakUser.email);
if (this.user.subscriptionId) {
this.user = await this.userService.getByMail(this.keycloakUser.email);
const originalKeycloakUser = await this.userService.getKeycloakUser(this.keycloakUser.id);
const priceId = originalKeycloakUser.attributes && originalKeycloakUser.attributes['priceID'] ? originalKeycloakUser.attributes['priceID'][0] : null;
if (priceId) {
originalKeycloakUser.attributes['priceID'] = null;
await this.userService.updateKeycloakUser(originalKeycloakUser);
}
if (!this.user.subscriptionPlan) {
if (this.id === 'free' || priceId === 'free') {
this.user.subscriptionPlan = 'free';
await this.userService.saveGuaranteed(this.user);
this.router.navigate([`/account`]);
} else if (this.id || priceId) {
const base64PriceId = this.id ? this.id : priceId;
this.checkout({ priceId: atob(base64PriceId), email: this.keycloakUser.email, name: `${this.keycloakUser.firstName} ${this.keycloakUser.lastName}` });
}
}
// if (this.id === 'free' || this.keycloakUser.priceId === 'free') {
// this.user.subscriptionPlan = 'free';
// await this.userService.saveGuaranteed(this.user);
// this.router.navigate([`/account`]);
// } else if (this.id || this.keycloakUser.priceId) {
// const priceId = this.id ? this.id : this.keycloakUser.priceId;
// this.checkout({ priceId: atob(priceId), email: this.keycloakUser.email, name: `${this.keycloakUser.firstName} ${this.keycloakUser.lastName}` });
// } else if (!this.id && !this.pricingOverview) {
// this.user = await this.userService.getByMail(this.keycloakUser.email);
// if (this.user.subscriptionId) {
// this.router.navigate([`/account`]);
// }
// }
} else {
this.pricingOverview = false;
}

View File

@@ -3,12 +3,12 @@ import { KeycloakService } from 'keycloak-angular';
import { User } from '../../../../../../bizmatch-server/src/models/db.model';
import { ErrorResponse, KeycloakUser, MailInfo } from '../../../../../../bizmatch-server/src/models/main.model';
import { environment } from '../../../../environments/environment';
import { MessageService } from '../../../components/message/message.service';
import { ValidatedInputComponent } from '../../../components/validated-input/validated-input.component';
import { ValidatedNgSelectComponent } from '../../../components/validated-ng-select/validated-ng-select.component';
import { ValidatedTextareaComponent } from '../../../components/validated-textarea/validated-textarea.component';
import { ValidationMessagesService } from '../../../components/validation-messages.service';
import { AuditService } from '../../../services/audit.service';
import { MailService } from '../../../services/mail.service';
import { SelectOptionsService } from '../../../services/select-options.service';
import { UserService } from '../../../services/user.service';
@@ -34,8 +34,9 @@ export class EmailUsComponent {
private validationMessagesService: ValidationMessagesService,
private messageService: MessageService,
public selectOptions: SelectOptionsService,
private auditService: AuditService,
) {
this.mailinfo = { sender: {}, email: '', url: environment.mailinfoUrl };
this.mailinfo = createMailInfo();
}
async ngOnInit() {
const token = await this.keycloakService.getToken();
@@ -50,9 +51,11 @@ export class EmailUsComponent {
}
async mail() {
try {
this.validationMessagesService.updateMessages([]);
this.mailinfo.email = 'support@bizmatch.net';
await this.mailService.mail(this.mailinfo);
this.messageService.addMessage({ severity: 'success', text: 'Your request has been forwarded to the support team of bizmatch.', duration: 3000 });
this.auditService.createEvent(null, 'emailus', this.mailinfo.email, this.mailinfo);
this.mailinfo = createMailInfo(this.user);
} catch (error) {
this.messageService.addMessage({

View File

@@ -47,6 +47,12 @@ export class UserService {
getNumberOfBroker(criteria?: UserListingCriteria): Observable<number> {
return this.http.post<number>(`${this.apiBaseUrl}/bizmatch/user/findTotal`, criteria);
}
getKeycloakUser(id: string): Promise<KeycloakUser> {
return lastValueFrom(this.http.get<KeycloakUser>(`${this.apiBaseUrl}/bizmatch/auth/users/${id}`));
}
async updateKeycloakUser(keycloakUser: KeycloakUser): Promise<void> {
await lastValueFrom(this.http.put<void>(`${this.apiBaseUrl}/bizmatch/auth/users/${keycloakUser.id}`, keycloakUser));
}
// -------------------------------
// ADMIN SERVICES
// -------------------------------

View File

@@ -137,9 +137,9 @@ export function resetUserListingCriteria(criteria: UserListingCriteria) {
criteria.radius = null;
}
export function createMailInfo(user: User): MailInfo {
export function createMailInfo(user?: User): MailInfo {
return {
sender: { name: `${user.firstname} ${user.lastname}`, email: user.email, phoneNumber: user.phoneNumber, state: user.location?.state, comments: null },
sender: user ? { name: `${user.firstname} ${user.lastname}`, email: user.email, phoneNumber: user.phoneNumber, state: user.location?.state, comments: null } : {},
email: null,
url: environment.mailinfoUrl,
listing: null,