2022-08-03 06:52:00 +00:00

206 lines
6.5 KiB
TypeScript

import { Injectable } from '@angular/core';
import { assign, cloneDeep } from 'lodash-es';
import { FuseMockApiService, FuseMockApiUtils } from '@fuse/lib/mock-api';
import {
loosings as loosingsData,
loosingReports as loosingReportsData,
} from './data';
@Injectable({
providedIn: 'root',
})
export class ReportLoosingMockApi {
private _loosings: any[] = loosingsData;
private _loosingReports: any[] = loosingReportsData;
/**
* Constructor
*/
constructor(private _fuseMockApiService: FuseMockApiService) {
// Register Mock API handlers
this.registerHandlers();
}
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* Register Mock API handlers
*/
registerHandlers(): void {
// -----------------------------------------------------------------------------------------------------
// @ Loosings - GET
// -----------------------------------------------------------------------------------------------------
this._fuseMockApiService
.onGet('api/apps/report/loosing/loosings', 300)
.reply(({ request }) => {
// Get available queries
const search = request.params.get('search');
// const sort = request.params.get('sort') || 'name';
// const order = request.params.get('order') || 'asc';
const page = parseInt(request.params.get('page') ?? '1', 10);
const size = parseInt(request.params.get('size') ?? '10', 10);
// Clone the loosings
let loosings: any[] | null = cloneDeep(this._loosings);
// If search exists...
if (search) {
// Filter the loosings
loosings = loosings.filter(
(contact: any) =>
contact.name &&
contact.name.toLowerCase().includes(search.toLowerCase())
);
}
// Paginate - Start
const loosingsLength = loosings.length;
// Calculate pagination details
const begin = page * size;
const end = Math.min(size * (page + 1), loosingsLength);
const lastPage = Math.max(Math.ceil(loosingsLength / size), 1);
// Prepare the pagination object
let pagination = {};
// If the requested page number is bigger than
// the last possible page number, return null for
// loosings but also send the last possible page so
// the app can navigate to there
if (page > lastPage) {
loosings = null;
pagination = {
lastPage,
};
} else {
// Paginate the results by size
loosings = loosings.slice(begin, end);
// Prepare the pagination mock-api
pagination = {
length: loosingsLength,
size: size,
page: page,
lastPage: lastPage,
startIndex: begin,
endIndex: end - 1,
};
}
// Return the response
return [
200,
{
loosings,
pagination,
},
];
});
// -----------------------------------------------------------------------------------------------------
// @ Loosing - GET
// -----------------------------------------------------------------------------------------------------
this._fuseMockApiService
.onGet('api/apps/report/loosing/loosing')
.reply(({ request }) => {
// Get the id from the params
const id = request.params.get('id');
// Clone the loosings
const loosingReports = cloneDeep(this._loosingReports);
// Find the loosing
// const loosing = loosings.find((item: any) => item.id === id);
// Return the response
return [200, loosingReports];
});
// -----------------------------------------------------------------------------------------------------
// @ Loosing - POST
// -----------------------------------------------------------------------------------------------------
this._fuseMockApiService
.onPost('api/apps/report/loosing/loosing')
.reply(() => {
// Generate a new loosing
const newLoosing = {
id: FuseMockApiUtils.guid(),
category: '',
name: 'A New User',
description: '',
tags: [],
sku: '',
barcode: '',
brand: '',
vendor: '',
stock: '',
reserved: '',
cost: '',
basePrice: '',
taxPercent: '',
price: '',
weight: '',
thumbnail: '',
images: [],
active: false,
};
// Unshift the new loosing
this._loosings.unshift(newLoosing);
// Return the response
return [200, newLoosing];
});
// -----------------------------------------------------------------------------------------------------
// @ Loosing - PATCH
// -----------------------------------------------------------------------------------------------------
this._fuseMockApiService
.onPatch('api/apps/report/loosing/loosing')
.reply(({ request }) => {
// Get the id and loosing
const id = request.body.id;
const loosing = cloneDeep(request.body.loosing);
// Prepare the updated loosing
let updatedLoosing = null;
// Find the loosing and update it
this._loosings.forEach((item, index, loosings) => {
if (item.id === id) {
// Update the loosing
loosings[index] = assign({}, loosings[index], loosing);
// Store the updated loosing
updatedLoosing = loosings[index];
}
});
// Return the response
return [200, updatedLoosing];
});
// -----------------------------------------------------------------------------------------------------
// @ Loosing - DELETE
// -----------------------------------------------------------------------------------------------------
this._fuseMockApiService
.onDelete('api/apps/report/loosing/loosing')
.reply(({ request }) => {
// Get the id
const id = request.params.get('id');
// Find the loosing and delete it
this._loosings.forEach((item, index) => {
if (item.id === id) {
this._loosings.splice(index, 1);
}
});
// Return the response
return [200, true];
});
}
}