This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
FoodLoop-Web/src/app/providers/api-service.ts

299 lines
6.1 KiB
TypeScript
Raw Normal View History

2017-05-09 13:39:48 +01:00
import { Injectable } from '@angular/core';
2017-11-15 14:16:05 +00:00
import { HttpClient } from '@angular/common/http';
2017-05-09 13:39:48 +01:00
import { Observable } from 'rxjs/Rx';
import { environment } from '../../environments/environment';
2017-05-09 13:39:48 +01:00
import 'rxjs/add/operator/map';
/* this provider handles the interaction between server and client */
2017-08-16 16:12:40 +01:00
2017-05-09 13:39:48 +01:00
@Injectable()
export class ApiService {
private apiUrl = environment.apiUrl;
2017-06-05 18:47:34 +01:00
private sessionKey: string = null;
2017-05-09 13:39:48 +01:00
constructor(
2017-11-15 14:12:28 +00:00
private http: HttpClient,
2017-06-05 18:47:34 +01:00
) {
if (localStorage.getItem('sessionKey') ) {
this.sessionKey = localStorage.getItem('sessionKey');
}
2017-06-05 18:47:34 +01:00
}
2017-08-16 16:12:40 +01:00
2017-09-07 15:35:55 +01:00
public post(url: string, data: any = {}) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + url,
data
2017-11-15 14:16:05 +00:00
);
}
2017-06-15 16:32:13 +01:00
// Login API
2017-08-16 16:12:40 +01:00
2017-08-29 18:15:47 +01:00
public getSessionKey() {
console.log('get key');
return this.sessionKey;
2017-06-05 18:47:34 +01:00
}
2017-08-16 16:12:40 +01:00
2017-08-29 18:15:47 +01:00
public setSessionKey(key) {
console.log('set key');
this.sessionKey = key;
localStorage.setItem('sessionKey', this.sessionKey);
2017-06-05 18:47:34 +01:00
}
2017-08-16 16:12:40 +01:00
2017-08-29 18:15:47 +01:00
public removeSessionKey() {
console.log('remove key');
this.sessionKey = null;
localStorage.removeItem('sessionKey');
2017-06-05 18:47:34 +01:00
}
2017-08-16 16:12:40 +01:00
2017-05-09 13:39:48 +01:00
public register(data) {
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
2017-05-09 13:39:48 +01:00
this.apiUrl + '/register',
data
2017-11-15 14:16:05 +00:00
);
2017-05-09 13:39:48 +01:00
}
public login(data) {
2017-09-08 14:59:31 +01:00
return this.http
2017-11-15 14:46:04 +00:00
.post<any>(
2017-09-08 14:59:31 +01:00
this.apiUrl + '/login',
data
)
.map(
result => {
2017-11-15 14:46:04 +00:00
const json = result;
2017-09-08 14:59:31 +01:00
this.setSessionKey(json.session_key);
this.setUserInfo(
json.email,
json.display_name || json.name
);
2017-09-08 14:59:31 +01:00
this.setUserType(json.user_type);
return json;
}
);
2017-05-09 13:39:48 +01:00
}
2017-08-16 16:12:40 +01:00
public logout() {
console.log(this.sessionKey);
const key = this.sessionKey;
return this.http
2017-11-15 14:46:04 +00:00
.post<any>(
this.apiUrl + '/logout',
{ session_key : key },
)
.map(
response => {
localStorage.clear();
this.sessionKey = null;
2017-11-15 14:46:04 +00:00
return response;
}
);
}
2017-05-09 13:39:48 +01:00
// Submits feedback
public feedback(data) {
data.app_name = 'Foodloop Web';
data.package_name = 'Foodloop Web';
data.version_code = 'dev';
data.version_number = 'dev';
console.log(data);
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/feedback',
data
2017-11-15 14:16:05 +00:00
);
}
// gets transaction list for log
public transList(data) {
const key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/outgoing-transactions',
{
session_key : key,
page : data
}
2017-11-15 14:16:05 +00:00
);
}
2017-08-29 18:15:47 +01:00
// Searches organisations used for transaction submission
2017-05-09 13:39:48 +01:00
public search(data) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/search',
data
2017-11-15 14:16:05 +00:00
);
2017-05-09 13:39:48 +01:00
}
2017-08-29 18:15:47 +01:00
// Uploads a transaction
2017-08-29 18:15:47 +01:00
public upload(data) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
2017-08-29 18:15:47 +01:00
this.apiUrl + '/upload',
data
2017-11-15 14:16:05 +00:00
);
2017-08-29 18:15:47 +01:00
}
2017-08-16 16:12:40 +01:00
// gets payroll list for log
public payrollList(data) {
const key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/v1/organisation/payroll',
{
session_key : key,
page : data
}
2017-11-15 14:16:05 +00:00
);
}
// handles Org data added
public orgPayroll(data) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/v1/organisation/payroll/add',
data
2017-11-15 14:16:05 +00:00
);
}
public orgSupplier(data) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/v1/organisation/supplier/add',
data
2017-11-15 14:16:05 +00:00
);
}
public orgEmployee(data) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/v1/organisation/employee/add',
data
2017-11-15 14:16:05 +00:00
);
}
// Handles user data interaction
2017-08-16 16:12:40 +01:00
// Checks for login status
public hasLoggedIn() {
return this.getSessionKey() ? true : false;
}
// Pulls user info to store locally on login
public setUserInfo(
email: string,
display_name: string) {
console.log('set UserInfo');
localStorage.setItem('email', email);
localStorage.setItem('displayname', display_name);
}
2017-08-16 16:12:40 +01:00
// Sets usertype
public setUserType(user_type: string) {
console.log('set UserType');
localStorage.setItem('usertype', user_type);
}
// Used for getting account details and updating
2017-08-16 16:12:40 +01:00
public accountFullLoad() {
const key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/user',
{ session_key : key },
2017-11-15 14:16:05 +00:00
);
}
2017-08-16 16:12:40 +01:00
public accountEditUpdate(data) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/user/account',
data
2017-11-15 14:16:05 +00:00
);
}
// Deletes account details on logout
public removeUserInfo() {
console.log('remove UserInfo');
localStorage.removeItem('email');
localStorage.removeItem('displayname');
}
public getFullName() {
console.log('get Full Name');
localStorage.getItem('fullname');
}
public getDisplayName() {
console.log('get Display Name');
localStorage.getItem('displayname');
}
public getPostcode() {
console.log('get Postcode');
localStorage.getItem('postcode');
}
public getYearOfBirth() {
console.log('get Year of Birth');
localStorage.getItem('yearofbirth');
}
public getEmail() {
console.log('get email');
localStorage.getItem('email');
}
2017-08-16 16:12:40 +01:00
2017-06-15 16:32:13 +01:00
// Leaderboard Api
2017-08-16 16:12:40 +01:00
public leaderboard_fetch(
type: string,
page: number) {
const key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/stats/leaderboard/paged',
{
session_key : key,
type : type,
page: page,
}
2017-11-15 14:16:05 +00:00
);
}
2017-08-16 16:12:40 +01:00
2017-09-27 13:54:10 +01:00
// Initial Map Data
public getMapData(data) {
data.session_key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/v1/supplier/location',
2017-09-27 13:54:10 +01:00
data
2017-11-15 14:16:05 +00:00
);
2017-09-27 13:54:10 +01:00
}
// Load LIS Data
public getLisData(data) {
data.session_key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/v1/supplier/location/lis',
data
);
}
// Basic Customer User stats API
public basicStats() {
const key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
this.apiUrl + '/stats',
{
session_key : key,
}
2017-11-15 14:16:05 +00:00
);
}
2017-08-16 16:12:40 +01:00
}