Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Dec 22, 2016
1 parent 85f3421 commit cda9250
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/client/app/app.module.ts
@@ -1,8 +1,9 @@
import { NgModule } from '@angular/core';
import { RequestOptions, Http, XHRBackend} from '@angular/http';
import { CurrencyPipe } from '@angular/common';

import { IonicApp, IonicModule } from 'ionic-angular';
import { Ng2Webstorage } from 'ng2-webstorage';
import { Ng2Webstorage, LocalStorageService } from 'ng2-webstorage';

import { MyAppComponent } from './app.component';
import { HomePageComponent } from '../pages/home/home';
Expand Down Expand Up @@ -59,6 +60,8 @@ import { InvoiceService } from '../services/invoice.service';
import { InventoryService } from '../services/inventory.service';
import { OrganizationalUnitService } from '../services/organizationalunit.service';

import { HttpClient } from '../services/http.custom';

import { PaginationComponent } from 'ionic2-pagination';

import { CurrencyFromSettingsPipe } from '../pipes/currency-from-settings';
Expand Down Expand Up @@ -152,7 +155,8 @@ import { TruncatePipe } from '../pipes/truncate';
InvoiceService,
InventoryService,
OrganizationalUnitService,
CurrencyPipe
CurrencyPipe,
HttpClient
]
})
export class AppModule {}
8 changes: 8 additions & 0 deletions src/client/pages/settings/edit/editsettings.html
Expand Up @@ -16,6 +16,14 @@
<ion-list>

<ion-list-header>Regional Settings</ion-list-header>
<ion-item>
<ion-label stacked>Location Name</ion-label>
<ion-input type="text" [(ngModel)]="settings.application.locationName"></ion-input>
</ion-item>
<ion-item>
<ion-label stacked>Terminal Identifier</ion-label>
<ion-input type="text" [(ngModel)]="settings.application.terminalId"></ion-input>
</ion-item>
<ion-item>
<ion-label stacked>Tax Rate (%)</ion-label>
<ion-input type="number" [(ngModel)]="settings.application.taxRate"></ion-input>
Expand Down
76 changes: 76 additions & 0 deletions src/client/services/http.custom.ts
@@ -0,0 +1,76 @@
import * as _ from 'lodash';

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptionsArgs, Request, Response, RequestOptions, RequestMethod } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { LocalStorageService } from 'ng2-webstorage';

@Injectable()
export class HttpClient {

private locationName: string;
private terminalId: string;

constructor(private http: Http,
private localStorage: LocalStorageService) {
this.localStorage.observe('locationName').subscribe(v => this.locationName = v);
this.localStorage.observe('terminalId').subscribe(v => this.terminalId = v);
}

private _setCustomHeaders(options?: RequestOptionsArgs): RequestOptionsArgs {

if(!options) {
options = new RequestOptions({});
}

if(!options.headers) {
options.headers = new Headers();
}

if(this.locationName) {
options.headers.set('X-Location', this.locationName);
}

if(this.terminalId) {
options.headers.set('X-Terminal', this.terminalId);
}

return options;
}

get(url, options?) {
const reqOpts = { method: RequestMethod.Get };
_.extend(reqOpts, options);
return this.request(url, reqOpts);
}

delete(url, options?) {
const reqOpts = { method: RequestMethod.Delete };
_.extend(reqOpts, options);
return this.request(url, reqOpts);
}

post(url, body, options?) {
const reqOpts = { method: RequestMethod.Post, body };
_.extend(reqOpts, options);
return this.request(url, reqOpts);
}

put(url, body, options?) {
const reqOpts = { method: RequestMethod.Put, body };
_.extend(reqOpts, options);
return this.request(url, reqOpts);
}

patch(url, body, options?) {
const reqOpts = { method: RequestMethod.Patch, body };
_.extend(reqOpts, options);
return this.request(url, reqOpts);
}

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
options = this._setCustomHeaders(options);
return this.http.request(url, options);
}
}
7 changes: 3 additions & 4 deletions src/client/services/inventory.service.ts
@@ -1,21 +1,20 @@

import * as _ from 'lodash';

import { StockItem } from '../models/stockitem';

import { LoggerService } from './logger.service';
import { ApplicationSettingsService } from './settings.service';

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Response } from '@angular/http';
import { HttpClient } from './http.custom';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class InventoryService {

private url = 'inventory';

constructor(private http: Http,
constructor(private http: HttpClient,
private logger: LoggerService,
private settings: ApplicationSettingsService) {}

Expand Down
7 changes: 3 additions & 4 deletions src/client/services/invoice.service.ts
@@ -1,22 +1,21 @@

import * as _ from 'lodash';

import { PagedItems } from '../models/pageditems';
import { Invoice } from '../models/invoice';

import { LoggerService } from './logger.service';
import { ApplicationSettingsService } from './settings.service';

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Response } from '@angular/http';
import { HttpClient } from './http.custom';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class InvoiceService {

private url = 'invoice';

constructor(private http: Http,
constructor(private http: HttpClient,
private logger: LoggerService,
private settings: ApplicationSettingsService) {}

Expand Down
7 changes: 3 additions & 4 deletions src/client/services/organizationalunit.service.ts
@@ -1,21 +1,20 @@

import * as _ from 'lodash';

import { OrganizationalUnit } from '../models/organizationalunit';

import { LoggerService } from './logger.service';
import { ApplicationSettingsService } from './settings.service';

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Response } from '@angular/http';
import { HttpClient } from './http.custom';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class OrganizationalUnitService {

private url = 'organizationalunit';

constructor(private http: Http,
constructor(private http: HttpClient,
private logger: LoggerService,
private settings: ApplicationSettingsService) {}

Expand Down
5 changes: 3 additions & 2 deletions src/client/services/promotion.service.ts
Expand Up @@ -10,15 +10,16 @@ import { LoggerService } from './logger.service';
import { ApplicationSettingsService } from './settings.service';

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Response } from '@angular/http';
import { HttpClient } from './http.custom';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class PromotionService {

private url = 'promotion';

constructor(private http: Http,
constructor(private http: HttpClient,
private logger: LoggerService,
private settings: ApplicationSettingsService) {}

Expand Down
28 changes: 23 additions & 5 deletions src/client/services/settings.service.ts
Expand Up @@ -4,8 +4,9 @@ import * as _ from 'lodash';
import { LoggerService } from './logger.service';

import { Injectable } from '@angular/core';
import { URLSearchParams, Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { URLSearchParams, Response } from '@angular/http';
import { LocalStorageService } from 'ng2-webstorage';
import { HttpClient } from './http.custom';

const CONNECTION_URL = 'http://localhost:8080';

Expand All @@ -26,6 +27,8 @@ export class Settings {

return !_.isUndefined(application.currencyCode) && application.currencyCode.length > 0
&& application.taxRate >= 0
&& application.locationName
&& application.terminalId

&& !_.isUndefined(db.hostname) && db.hostname.length > 0
&& !_.isUndefined(db.username) && db.username.length > 0
Expand All @@ -41,11 +44,20 @@ export class ApplicationSettingsService {

private url = 'system';

constructor(private http: Http,
constructor(private http: HttpClient,
private localStorage: LocalStorageService,
private logger: LoggerService) {
this.getAllSettings();
}

get terminalId(): string {
return this.settings.application.terminalId;
}

get locationName(): string {
return this.settings.application.locationName;
}

get currencyCode(): string {
return this.settings.application.currencyCode;
}
Expand All @@ -66,13 +78,19 @@ export class ApplicationSettingsService {
return this.settings.isValid;
}

mergeSettings(newSettings): void {
_.merge(this.settings, newSettings);
this.localStorage.store('locationName', this.settings.application.locationName);
this.localStorage.store('terminalId', this.settings.application.terminalId);
}

getAllSettings(): void {
this.http.get(this.buildAPIURL(this.url))
.map((res: Response) => this.logger.observableUnwrap(res.json()))
.catch(e => this.logger.observableError(e))
.toPromise()
.then(data => {
_.merge(this.settings, data);
this.mergeSettings(data);
});
}

Expand All @@ -82,7 +100,7 @@ export class ApplicationSettingsService {
.catch(e => this.logger.observableError(e))
.toPromise()
.then(data => {
_.merge(this.settings, data);
this.mergeSettings(data);
return this.settings;
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/client/services/stockitem.service.ts
Expand Up @@ -8,15 +8,16 @@ import { LoggerService } from './logger.service';
import { ApplicationSettingsService } from './settings.service';

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Response } from '@angular/http';
import { HttpClient } from './http.custom';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class StockItemService {

private url = 'stockitem';

constructor(private http: Http,
constructor(private http: HttpClient,
private logger: LoggerService,
private settings: ApplicationSettingsService) {}

Expand Down Expand Up @@ -71,7 +72,7 @@ export class StockItemService {
}

exportMany(items: StockItem[]): Observable<any> {
return this.http.post(this.settings.buildAPIURL(`${this.url}/export`), this.transformItemsToHash(items))
return this.http.post(this.settings.buildAPIURL(`${this.url}/import`), this.transformItemsToHash(items))
.map((res: Response) => this.logger.observableUnwrap(res.json()))
.catch(e => this.logger.observableError(e));
}
Expand Down
1 change: 1 addition & 0 deletions src/server/routes/stockitem.ts
Expand Up @@ -17,6 +17,7 @@ const cleanItem = (item) => {

export default (app) => {
app.get('/stockitem', (req, res) => {
console.log(req.headers);

const pageOpts = {
pageSize: +req.query.pageSize || Settings.pagination.pageSize,
Expand Down
9 changes: 8 additions & 1 deletion src/server/server.ts
Expand Up @@ -44,7 +44,14 @@ export const start = () => {
const cors = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(cors());

const corsOptions = {
methods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'],
allowedHeaders: ['X-Location', 'X-Terminal', 'Content-Type'],
exposedHeaders: ['X-Location', 'X-Terminal', 'Content-Type']
};

app.use(cors(corsOptions));

require('./routes/index').default(app);

Expand Down

0 comments on commit cda9250

Please sign in to comment.