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' ;
2017-07-27 17:07:41 +01:00
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 {
2017-07-27 17:07:41 +01:00
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
) {
2017-09-08 14:49:57 +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 = { } ) {
2017-09-08 15:22:15 +01:00
data . session_key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-08-29 17:39:27 +01:00
this . apiUrl + url ,
data
2017-11-15 14:16:05 +00:00
) ;
2017-08-29 17:39:27 +01: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() {
2017-09-08 14:49:57 +01:00
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 ) {
2017-09-08 14:49:57 +01:00
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() {
2017-09-08 14:49:57 +01:00
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-07-28 19:30:35 +01:00
) ;
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
2017-09-08 14:49:57 +01:00
public logout() {
const key = this . sessionKey ;
2017-09-08 15:22:15 +01:00
return this . http
2017-11-15 14:46:04 +00:00
. post < any > (
2017-09-08 15:22:15 +01:00
this . apiUrl + '/logout' ,
{ session_key : key } ,
2018-06-04 15:23:16 +01:00
) . pipe (
map (
2017-09-08 15:22:15 +01:00
response = > {
localStorage . clear ( ) ;
this . sessionKey = null ;
2017-11-15 14:46:04 +00:00
return response ;
2017-09-08 15:22:15 +01:00
}
2018-06-04 15:23:16 +01:00
) ) ;
2017-09-08 14:49:57 +01:00
}
2017-05-09 13:39:48 +01:00
2017-09-01 14:36:31 +01:00
// Submits feedback
public feedback ( data ) {
data . app_name = 'Foodloop Web' ;
data . package_name = 'Foodloop Web' ;
2017-09-01 15:02:05 +01:00
data . version_code = 'dev' ;
data . version_number = 'dev' ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-09-01 14:36:31 +01:00
this . apiUrl + '/feedback' ,
data
2017-11-15 14:16:05 +00:00
) ;
2017-09-01 14:36:31 +01:00
}
2017-09-06 16:05:35 +01:00
// gets transaction list for log
public transList ( data ) {
2017-09-08 14:49:57 +01:00
const key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-09-08 14:49:57 +01:00
this . apiUrl + '/outgoing-transactions' ,
{
2017-09-06 16:05:35 +01:00
session_key : key ,
page : data
}
2017-11-15 14:16:05 +00:00
) ;
2017-09-06 16:05:35 +01: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 ,
}
) ;
}
2018-01-17 17:36:28 +00:00
// 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-08-31 18:44:17 +01:00
2017-05-09 13:39:48 +01:00
public search ( data ) {
2017-09-08 14:49:57 +01:00
data . session_key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-09-08 14:49:57 +01:00
this . apiUrl + '/search' ,
data
2017-11-15 14:16:05 +00:00
) ;
2017-05-09 13:39:48 +01:00
}
2017-08-31 18:44:17 +01:00
2017-08-29 18:15:47 +01:00
// Uploads a transaction
2017-08-31 18:44:17 +01:00
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
2018-03-20 17:29:00 +00: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
) ;
}
2018-03-20 17:29:00 +00:00
// Edits a recurring transaction
public recurDelete ( data ) {
data . session_key = this . sessionKey ;
return this . http . post < any > (
this . apiUrl + '/recurring-transactions/delete' ,
data
) ;
}
2017-09-19 15:38:35 +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 > (
2017-09-19 15:38:35 +01:00
this . apiUrl + '/v1/organisation/payroll' ,
{
session_key : key ,
page : data
}
2017-11-15 14:16:05 +00:00
) ;
2017-09-19 15:38:35 +01:00
}
2017-09-08 11:54:34 +01: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 > (
2017-09-19 15:38:35 +01:00
this . apiUrl + '/v1/organisation/payroll/add' ,
2017-09-08 11:54:34 +01:00
data
2017-11-15 14:16:05 +00:00
) ;
2017-09-08 11:54:34 +01:00
}
public orgSupplier ( data ) {
data . session_key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-09-19 15:38:35 +01:00
this . apiUrl + '/v1/organisation/supplier/add' ,
2017-09-08 11:54:34 +01:00
data
2017-11-15 14:16:05 +00:00
) ;
2017-09-08 11:54:34 +01:00
}
public orgEmployee ( data ) {
data . session_key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-09-19 15:38:35 +01:00
this . apiUrl + '/v1/organisation/employee/add' ,
2017-09-08 11:54:34 +01:00
data
2017-11-15 14:16:05 +00:00
) ;
2017-09-08 11:54:34 +01:00
}
2017-07-28 19:30:35 +01:00
// Handles user data interaction
2017-08-16 16:12:40 +01:00
2017-07-28 19:30:35 +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 ) {
2017-09-08 14:49:57 +01:00
localStorage . setItem ( 'email' , email ) ;
localStorage . setItem ( 'displayname' , display_name ) ;
2017-07-28 19:30:35 +01:00
}
2017-08-16 16:12:40 +01:00
2017-08-31 18:44:17 +01:00
// Sets usertype
public setUserType ( user_type : string ) {
2017-09-08 14:49:57 +01:00
localStorage . setItem ( 'usertype' , user_type ) ;
2017-08-31 18:44:17 +01:00
}
2017-07-28 19:30:35 +01:00
// Used for getting account details and updating
2017-08-16 16:12:40 +01:00
2017-07-28 19:30:35 +01:00
public accountFullLoad() {
2017-09-08 14:49:57 +01:00
const key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-07-28 19:30:35 +01:00
this . apiUrl + '/user' ,
{ session_key : key } ,
2017-11-15 14:16:05 +00:00
) ;
2017-07-28 19:30:35 +01:00
}
2017-08-16 16:12:40 +01:00
2017-07-28 19:30:35 +01:00
public accountEditUpdate ( data ) {
2017-09-08 14:49:57 +01:00
data . session_key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-07-28 19:30:35 +01:00
this . apiUrl + '/user/account' ,
data
2017-11-15 14:16:05 +00:00
) ;
2017-07-28 19:30:35 +01: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
2017-11-10 17:15:11 +00:00
public leaderboard_fetch (
type : string ,
page : number ) {
2017-09-08 14:49:57 +01:00
const key = this . sessionKey ;
2017-11-15 14:46:04 +00:00
return this . http . post < any > (
2017-11-10 17:15:11 +00:00
this . apiUrl + '/stats/leaderboard/paged' ,
2017-09-08 14:49:57 +01:00
{
session_key : key ,
2017-11-10 17:15:11 +00:00
type : type ,
page : page ,
2017-09-08 14:49:57 +01:00
}
2017-11-15 14:16:05 +00:00
) ;
2017-09-08 14:49:57 +01: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 > (
2017-09-28 12:47:09 +01:00
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 ) {
2017-11-23 17:16:46 +00:00
data . session_key = this . sessionKey ;
return this . http . post < any > (
2017-12-08 13:26:18 +00:00
this . apiUrl + '/v1/supplier/location/trail' ,
2017-11-23 17:16:46 +00:00
data
) ;
}
2017-08-31 18:44:17 +01:00
// Basic Customer User stats API
2017-12-18 12:57:40 +00:00
public customerStats() {
2017-09-08 14:49:57 +01:00
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' ,
2017-09-08 14:49:57 +01:00
{
2017-08-31 18:44:17 +01:00
session_key : key ,
2017-09-08 14:49:57 +01:00
}
2017-11-15 14:16:05 +00:00
) ;
2017-06-19 17:20:21 +01:00
}
2019-07-11 16:12:46 +01: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
}