h-usersite/angular/src/app/services/apple-wallet.service.ts
nikolay 9fd031f423 dev #14607 Море. Правки по сайту:
change agree info;
remove add to apple wallet notification;
fix apple wallet generation;
2023-07-04 15:45:43 +04:00

92 lines
2.4 KiB
TypeScript

import { lastValueFrom } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable, Inject } from '@angular/core';
import { environment } from 'src/environments/environment';
import { CookiesService } from './cookies.service';
import { RpcService, JsonrpcService } from 'src/app/services/jsonrpc.service';
import { DOCUMENT } from '@angular/common';
import { UserInfo } from 'src/app/interface/data';
@Injectable({
providedIn: 'root',
})
export class AppleWalletService {
private url: string = environment.appleWalletEndpoint;
constructor(
private http: HttpClient,
private cookiesService: CookiesService,
private jsonrpc: JsonrpcService,
@Inject(DOCUMENT) private document: Document,
) { }
generateCard(token: string, user_id: string) {
let headers = new HttpHeaders();
headers = headers.set('Authorization', environment.appleWalletSecret);
const options = {
headers: headers,
};
return this.http.get(`${this.url}/client/${environment.clientName}/passUrl/${user_id}/token/${token}`, options)
}
reloadCard(user_id: string) {
let headers = new HttpHeaders();
headers = headers.set('Authorization', environment.appleWalletSecret);
const options = {
headers: headers,
};
const body = {
text: '',
isUpdateCard: true
}
return this.http.post(`${this.url}/sendNotification/${user_id}`, body, options)
}
async addCardToWallet(userInfo: UserInfo) {
const token = this.cookiesService.getItem('token');
try {
this.jsonrpc.rpc(
{
method: 'updateAdditionalInfo',
params: [
{
first_name: userInfo.name,
birth_day: '01.01.1999'
},
],
},
RpcService.authService,
true
).subscribe({
next: async () => {
const accountData = (
await lastValueFrom(
this.jsonrpc.rpc(
{
method: 'getTokenData',
params: [],
},
RpcService.authService,
true
)
)
).data;
if (token && accountData.user_id) {
this.generateCard(token, accountData.user_id).subscribe({
next: (res: any) => {
this.document.location.href = res.url;
},
error: (err) => {
console.log('Error: ', err);
},
});
}
}});
} catch (e) {
console.log(e);
}
}
}