Files
doneit-web/src/app/services/notifications.service.ts
T

321 lines
12 KiB
TypeScript
Raw Normal View History

/* eslint-disable */
2021-07-08 14:45:38 +01:00
/* tslint:disable */
2021-06-25 09:35:50 +01:00
///<reference path="../../../plugins/cordova-plugin-mfp-push/typings/mfppush.d.ts" />
import { Injectable, NgZone } from '@angular/core';
2021-07-07 15:28:29 +01:00
import { ActivatedRoute } from '@angular/router'
2021-02-01 13:31:33 +01:00
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { environment } from 'src/environments/environment';
2021-04-12 08:34:17 +01:00
import { StorageService } from 'src/app/services/storage.service';
import { AuthConnstants } from 'src/app/config/auth-constants';
2021-02-01 13:31:33 +01:00
import { Token } from '../models/token.model';
2021-04-12 15:39:44 +01:00
import { ModalController, AlertController, AnimationController, Platform } from '@ionic/angular';
2021-06-25 09:35:50 +01:00
import { NavigationExtras,Router } from '@angular/router';
import { ToastService } from '../services/toast.service';
2021-07-08 10:43:40 +01:00
import { Optional } from '@angular/core';
2021-07-29 16:02:39 +01:00
import { JsonStore } from './jsonStore.service';
2021-08-30 10:24:46 +01:00
import { synchro } from './socket/synchro.service';
2021-02-01 13:31:33 +01:00
@Injectable({
providedIn: 'root'
})
export class NotificationsService {
2021-04-12 08:34:17 +01:00
adding: "intervenient" | "CC" = "intervenient";
folderId: string;
2021-07-31 22:04:09 +01:00
DataArray: Array<String> = [];
2021-04-12 08:34:17 +01:00
2021-08-25 11:51:02 +01:00
callbacks: {
type: string,
funx: Function
}[] = []
2021-04-12 08:34:17 +01:00
constructor(
2021-07-08 10:43:40 +01:00
private http: HttpClient,
2021-04-12 08:34:17 +01:00
private storageService: StorageService,
2021-04-12 15:39:44 +01:00
private modalController: ModalController,
2021-04-12 08:34:17 +01:00
public modalCtrl: AlertController,
2021-04-12 15:39:44 +01:00
private animationController: AnimationController,
2021-06-25 09:35:50 +01:00
private platform: Platform,
2021-07-08 10:43:40 +01:00
private router: Router,
2021-06-25 09:35:50 +01:00
private toastService: ToastService,
2021-07-07 15:28:29 +01:00
private zone: NgZone,
2021-07-29 16:02:39 +01:00
private activeroute: ActivatedRoute,
2021-08-31 13:29:26 +01:00
private jsonstore: JsonStore) {
this.storageService.get("Notifications").then((value) => {
2021-08-31 14:43:03 +01:00
}).catch(()=>{
this.storageService.store("Notifications",[])
2021-08-31 13:29:26 +01:00
})
}
2021-08-25 11:51:02 +01:00
2021-08-30 10:24:46 +01:00
registerCallback(type: string, funx: Function, object: any = {} ) {
2021-08-25 11:51:02 +01:00
2021-08-30 10:24:46 +01:00
this.callbacks.push({type, funx})
2021-08-31 19:30:08 +01:00
synchro.registerCallback('Notification',funx, type)
2021-08-30 10:24:46 +01:00
}
2021-02-01 13:31:33 +01:00
getTokenByUserIdAndId(user, userID) {
const geturl = environment.apiURL + 'notifications/user/' + userID;
return this.http.get<Token[]>(`${geturl}`);
}
2021-06-25 09:35:50 +01:00
getAndpostToken(username) {
2021-07-29 16:02:39 +01:00
2021-06-25 14:37:52 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
2021-06-25 09:35:50 +01:00
console.log('Notifications not supported')
2021-04-12 15:39:44 +01:00
} else {
2021-04-12 08:34:17 +01:00
2021-06-25 09:35:50 +01:00
const geturl = environment.apiURL + 'notifications/token';
2021-08-13 17:31:01 +01:00
if(window['WLAuthorizationManager']) {
if(window['WLAuthorizationManager'].obtainAccessToken) {
window['WLAuthorizationManager'].obtainAccessToken("push.mobileclient").then(
(token) => {
console.log('Push Notification: Success ' + token);
window['MFPPush'].initialize(
function (successResponse) {
console.log("Push notification Successfully Service intialized: " + successResponse);
},
function (failureResponse) {
console.log("Push notification failure Service intialized: " + failureResponse);
}
);
window['MFPPush'].registerDevice(null, (successResponse) => {
console.log("Successfully registered: " + JSON.stringify(successResponse));
console.log('token: ', successResponse.deviceId)
this.storageService.store(username, successResponse.deviceId);
this.storageService.get(username).then(value => {
console.log('STORAGE TOKEN', value)
this.storageService.get(AuthConnstants.USER).then(res => {
console.log('USERID', res);
const headers = { 'Authorization': 'Basic cGF1bG8ucGludG9AZ2FiaW5ldGVkaWdpdGFsLmxvY2FsOnRhYnRlc3RlQDAwNg==' };
const body = {
UserId: res.UserId,
TokenId: successResponse.deviceId,
Status: 1,
Service: 1
};
this.http.post<Token>(`${geturl}`, body, { headers }).subscribe(data => {
console.log('TOKEN USER MIDLE', data);
})
});
2021-08-13 16:39:28 +01:00
});
2021-08-13 17:31:01 +01:00
},
function (failureResponse) {
console.log("Successfully failue: " + JSON.stringify(failureResponse));
}
);
}, (error) => {
console.log('Push notification recived: failure ' + error.responseText);
console.log(JSON.stringify(error));
}
);
}
2021-08-13 16:39:28 +01:00
}
2021-06-25 09:35:50 +01:00
2021-08-13 17:31:01 +01:00
2021-04-12 08:34:17 +01:00
}
}
2021-07-31 22:04:09 +01:00
getAndpostToken2() {
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
console.log('Notifications not supported')
} else {
const geturl = environment.apiURL + 'notifications/token';
2021-08-13 17:31:01 +01:00
if(window['WLAuthorizationManager']) {
if(window['WLAuthorizationManager'].obtainAccessToken) {
window['WLAuthorizationManager'].obtainAccessToken("push.mobileclient").then(
(token) => {
console.log('Push Notification: Success ' + token);
window['MFPPush'].initialize(
function (successResponse) {
console.log("Push notification Successfully Service intialized: " + successResponse);
},
function (failureResponse) {
console.log("Push notification failure Service intialized: " + failureResponse);
}
);
window['MFPPush'].registerDevice(null, (successResponse) => {
console.log("Successfully registered: " + JSON.stringify(successResponse));
console.log('token: ', successResponse.deviceId)
/* this.storageService.store(username, successResponse.deviceId);
this.storageService.get(username).then(value => {
console.log('STORAGE TOKEN', value)
this.storageService.get(AuthConnstants.USER).then(res => {
console.log('USERID', res);
const headers = { 'Authorization': 'Basic cGF1bG8ucGludG9AZ2FiaW5ldGVkaWdpdGFsLmxvY2FsOnRhYnRlc3RlQDAwNg==' };
const body = {
UserId: res.UserId,
TokenId: successResponse.deviceId,
Status: 1,
Service: 1
};
this.http.post<Token>(`${geturl}`, body, { headers }).subscribe(data => {
console.log('TOKEN USER MIDLE', data);
})
});
}); */
},
function (failureResponse) {
console.log("Successfully failue: " + JSON.stringify(failureResponse));
}
);
}, (error) => {
console.log('Push notification recived: failure ' + error.responseText);
console.log(JSON.stringify(error));
}
2021-08-30 12:13:06 +01:00
)
2021-08-30 12:07:14 +01:00
} else {
console.log('not called')
2021-08-13 17:31:01 +01:00
}
2021-08-30 12:07:14 +01:00
} else {
console.log('not called')
}
2021-08-13 17:31:01 +01:00
}
}
2021-08-18 17:36:40 +01:00
removeDepartment(index): void {
/* this.DataArray[this.DataArray.findIndex(item => item.index == index)];
this.DataArray.splice( this.DataArray.findIndex(item => item.indexOf(index.to) === 'William'),1); */
const indexx = this.DataArray.indexOf(index, 0);
if (indexx > -1) {
this.DataArray.splice(index, 1);
console.log('This notificatio', this.DataArray);
}
}
tempClearArray(){
this.DataArray = [];
}
2021-08-13 17:31:01 +01:00
async onReceviNotification() {
if(window['WLAuthorizationManager']) {
2021-08-13 16:39:28 +01:00
if(window['WLAuthorizationManager'].obtainAccessToken) {
window['WLAuthorizationManager'].obtainAccessToken("push.mobileclient").then(
(token) => {
console.log('Push Notification: Success ' + token);
2021-08-13 17:31:01 +01:00
2021-08-13 16:39:28 +01:00
window['MFPPush'].initialize(
function (successResponse) {
2021-08-13 17:31:01 +01:00
console.log("Push notification Successfully intialized: " + successResponse);
window['MFPPush'].registerNotificationsCallback(notificationReceived);
2021-08-13 16:39:28 +01:00
},
function (failureResponse) {
2021-08-13 17:31:01 +01:00
console.log("Push notification failure intialized: " + failureResponse);
2021-08-13 16:39:28 +01:00
}
);
2021-08-13 17:31:01 +01:00
var notificationReceived = (message) => {
//this.jsonstore.createCollection('Notifications',message);
this.DataArray.push(message)
2021-08-19 13:58:43 +01:00
console.log("On ReceiveNotification", this.DataArray)
2021-08-30 14:55:24 +01:00
this.storageService.store("Notifications",this.DataArray)
2021-08-13 17:31:01 +01:00
console.log(message);
var data = JSON.parse(message.payload);
2021-08-30 10:24:46 +01:00
synchro.$send(data)
console.log('data.Service', data.Service); // module
console.log('data.IdObject', data.IdObject); // Object id
console.log('data.Object', data.Object); // details
2021-08-13 17:31:01 +01:00
if(message.actionName){
this.notificatinsRoutes(data);
} else {
2021-08-18 17:36:40 +01:00
/* this.toastService.notificationMessage(message.alert,this.notificatinsRoutes, data); */
//this.notificatinsRoutes(data);
console.log(data)
2021-08-13 16:39:28 +01:00
}
this.callbacks.forEach( e=> {
2021-08-30 14:55:24 +01:00
if(e.type == data.Object || e.type == "any") {
e.funx()
}
})
2021-08-13 17:31:01 +01:00
}
2021-08-13 16:39:28 +01:00
}, (error) => {
console.log('Push notification recived: failure ' + error.responseText);
console.log(JSON.stringify(error));
}
);
}
}
2021-07-31 22:04:09 +01:00
}
2021-06-25 09:35:50 +01:00
notificatinsRoutes = (data) => {
if (data.Service === "agenda") {
this.zone.run(() => this.router.navigate(['/home/agenda', data.IdObject, 'agenda']));
}
else if (data.Service === "gabinete-digital" && data.Object === "expediente") {
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/expediente',data.IdObject,'gabinete-digital']));
}
else if (data.Service === "agenda" && data.Object === "event-list") {
this.zone.run(() => this.router.navigate(['/home/agenda/event-list/approve-event',data.IdObject, 'agenda']));
2021-06-25 14:37:52 +01:00
}else if (data.Service === "gabinete-digital" && data.Object === "despachos") {
2021-07-30 16:06:58 +01:00
2021-07-07 15:28:29 +01:00
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/despachos',data.IdObject,'gabinete-digital'],{replaceUrl: true}));
2021-06-25 09:35:50 +01:00
}
else if (data.Service === "gabinete-digital" && data.Object === "parecer") {
2021-07-07 15:28:29 +01:00
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos',data.IdObject,'gabinete-digital']));
2021-06-25 09:35:50 +01:00
}
else if (data.Service === "gabinete-digital" && data.Object === "deferimento") {
2021-07-30 16:06:58 +01:00
2021-07-07 15:28:29 +01:00
this.zone.run(() => this.router.navigate(['/home/gabinete-digital/pedidos',data.IdObject,'gabinete-digital']));
2021-06-25 09:35:50 +01:00
}
2021-07-06 09:02:24 +01:00
else if (data.Service === "gabinete-digital" && data.Object === "despachos-pr") {
2021-07-30 16:06:58 +01:00
this.zone.run(() =>this.router.navigate(['/home/gabinete-digital/despachos-pr',data.IdObject,'gabinete-digital']));
2021-06-25 09:35:50 +01:00
}
else if (data.Service === "accoes" && data.Object === "accao") {
this.zone.run(() => this.router.navigate(['/home/publications',data.IdObject]));
}
else if (data.Service === "accoes" && data.Object === "publicacao") {
this.zone.run(() => this.router.navigate(['/home/publications/view-publications',data.FolderId,data.IdObject]));
}
2021-07-06 16:17:03 +01:00
else if (data.Service === "gabinete-digital" && data.Object === "diplomas") {
this.zone.run(() =>this.router.navigate(['/home/gabinete-digital/diplomas', data.IdObject, 'gabinete-digital']));
}
else if (data.Service === "gabinete-digital" && data.Object === "diplomas-assinar") {
let navigationExtras: NavigationExtras = {
queryParams: {
"serialNumber": data.IdObject,
}
};
this.zone.run(() =>this.router.navigate(['/home/gabinete-digital/diplomas-assinar/diploma-assinar'], navigationExtras));
}
else if (data.Service === "gabinete-digital" && data.Object === "expedientes-pr") {
this.zone.run(() =>this.router.navigate(['/home/gabinete-digital/expedientes-pr',data.IdObject,'gabinete-digital']));
}
2021-04-12 08:34:17 +01:00
}
2021-06-25 09:35:50 +01:00
platformVerify() {
2021-04-12 08:34:17 +01:00
2021-06-25 09:35:50 +01:00
if (this.platform.is('cordova')) {
} else {
2021-04-12 08:34:17 +01:00
}
2021-04-12 15:39:44 +01:00
}
2021-06-25 09:35:50 +01:00
2021-02-01 13:31:33 +01:00
}