This repository has been archived on 2023-08-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
FoodLoop-Web/src/app/providers/api-service.ts

374 lines
7.5 KiB
TypeScript
Raw Normal View History

2018-06-04 15:23:16 +01:00
import { map } from 'rxjs/operators';
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';
import { environment } from '../../environments/environment';
2018-06-04 15:23:16 +01:00
2017-05-09 13:39:48 +01:00
/* 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() {
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) {
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() {
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
2018-06-04 15:23:16 +01:00
).pipe(
map(
2017-09-08 14:59:31 +01:00
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;
}
2018-06-04 15:23:16 +01:00
));
2017-05-09 13:39:48 +01:00
}
2017-08-16 16:12:40 +01:00
public logout() {
const key = this.sessionKey;
return this.http
2017-11-15 14:46:04 +00:00
.post<any>(
this.apiUrl + '/logout',
{ session_key : key },
2018-06-04 15:23:16 +01:00
).pipe(
map(
response => {
localStorage.clear();
this.sessionKey = null;
2017-11-15 14:46:04 +00:00
return response;
}
2018-06-04 15:23:16 +01:00
));
}
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';
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
);
}
2018-01-15 14:19:16 +00:00
// Basic Customer User stats API
public categoryList() {
const key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/search/category',
{
session_key : key,
}
);
}
// Basic Customer User stats API
public categoryTransactionList() {
const key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/stats/category',
{
session_key : key,
}
);
}
2019-07-11 15:17:50 +01:00
// LCC data
public externalTransactions() {
const key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/v1/organisation/external/transactions',
{
session_key : key,
}
);
}
2019-07-15 12:38:06 +01:00
public loadMiscUrl(extra_url) {
const key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/v1/' + extra_url,
{
session_key : key,
}
);
}
public externalSuppliers(data, sortBy, sortDir, perPage) {
2019-07-11 15:17:50 +01:00
const key = this.sessionKey;
return this.http.post<any>(
2019-07-12 18:50:59 +01:00
this.apiUrl + '/v1/organisation/external/suppliers',
2019-07-11 15:17:50 +01:00
{
session_key : key,
2019-07-12 20:05:06 +01:00
page : data,
sort_by : sortBy,
2019-07-15 12:38:06 +01:00
sort_dir : sortDir,
per_page : perPage
2019-07-11 15:17:50 +01: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
// Edits a recurring transaction
2018-03-20 16:53:29 +00:00
public recurUpdate(data) {
data.session_key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/recurring-transactions',
data
);
}
// Edits a recurring transaction
public recurDelete(data) {
data.session_key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/recurring-transactions/delete',
data
);
}
// 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) {
localStorage.setItem('email', email);
localStorage.setItem('displayname', display_name);
}
2017-08-16 16:12:40 +01:00
// Sets usertype
public setUserType(user_type: string) {
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() {
localStorage.removeItem('email');
localStorage.removeItem('displayname');
}
public getFullName() {
localStorage.getItem('fullname');
}
public getDisplayName() {
localStorage.getItem('displayname');
}
public getPostcode() {
localStorage.getItem('postcode');
}
public getYearOfBirth() {
localStorage.getItem('yearofbirth');
}
public getEmail() {
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
}
2017-12-08 13:26:18 +00:00
// Load Association Data
public getAssocData(data) {
data.session_key = this.sessionKey;
return this.http.post<any>(
2017-12-08 13:26:18 +00:00
this.apiUrl + '/v1/supplier/location/trail',
data
);
}
// Basic Customer User stats API
2017-12-18 12:57:40 +00:00
public customerStats() {
const key = this.sessionKey;
2017-11-15 14:46:04 +00:00
return this.http.post<any>(
2017-12-18 12:57:40 +00:00
this.apiUrl + '/stats/customer',
{
session_key : key,
}
2017-11-15 14:16:05 +00:00
);
}
// Basic Customer User stats API
public orgStats() {
const key = this.sessionKey;
return this.http.post<any>(
this.apiUrl + '/stats/organisation',
{
session_key : key,
}
);
}
2017-08-16 16:12:40 +01:00
}