Skip to content

Commit 471ab76

Browse files
Merge pull request #50 from akbarsaputrait/fix-49
[FIX #49] Owner - Chart sales & orders
2 parents 2d4c6a2 + 4de01a7 commit 471ab76

File tree

8 files changed

+52
-17
lines changed

8 files changed

+52
-17
lines changed

src/app.controller.ts

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Quero } from '@core/decorators/quero.decorator';
2-
import { Owner } from '@db/entities/owner/owner.entity';
32
import base64url from '@lib/helpers/base64.helper';
43
import { config } from '@lib/helpers/config.helper';
5-
import AppDataSource from '@lib/typeorm/datasource.typeorm';
64
import { Controller, Get, Res } from '@nestjs/common';
75
import * as fs from 'fs';
86
import { lookup } from 'mime-types';
@@ -12,9 +10,6 @@ import * as path from 'path';
1210
export class AppController {
1311
@Get()
1412
async getHello(@Res() response) {
15-
const check = await AppDataSource.createQueryBuilder(Owner, 't1').getCount();
16-
console.log({ check });
17-
1813
return response.send('Ordero API V2');
1914
}
2015

src/app/customer/order/order.controller.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ export class OrderController {
101101
orderProduct = checkOrderProduct;
102102
}
103103

104+
stock.allocated += product.qty;
105+
await manager.getRepository(ProductStock).save(stock);
106+
104107
orderProduct.order_id = order.id;
105108
orderProduct.product_variant_id = variant.id;
106109
orderProduct.qty = product.qty;
@@ -112,9 +115,11 @@ export class OrderController {
112115
}
113116

114117
// Updare Order Number
118+
const totalPrice = orderedProducts.reduce((price, a) => price + a.price, 0);
115119
await manager.getRepository(Order).update(order.id, {
116120
number: sequenceNumber(order.uid),
117-
gross_total: orderedProducts.reduce((price, a) => price + a.price, 0),
121+
gross_total: totalPrice,
122+
net_total: totalPrice,
118123
});
119124

120125
table.status = TableStatus.InUse;

src/app/owner/restaurant/dashboard/dashboard.controller.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ export class DashboardController {
3030
return response.data({ id: uuid(), data });
3131
}
3232

33+
@Get('orders/chart')
34+
@Permissions(`${PermOwner.Dashboard}@${PermAct.R}`)
35+
async orderChart(@Rest() rest, @Loc() loc: Location, @Res() response) {
36+
const query = AppDataSource.createQueryBuilder(Order, 't1')
37+
.where({ restaurant_id: rest.id })
38+
.groupBy('t1.created_at')
39+
.selectWithAlias(['_IFNULL(COUNT(t1.id), 0) AS total_order', 'created_at']);
40+
41+
if (loc) {
42+
query.andWhere({ location_id: loc.id });
43+
}
44+
const orders = await query.search().dateRange().getRawMany();
45+
46+
const { start, end } = AppDataSource.createQueryBuilder(Order, 't1').dateRange().getParameters();
47+
const results = await getChartData({ start, end }, orders, 'total_order');
48+
49+
return response.data(results);
50+
}
51+
3352
@Get('sales/total')
3453
@Permissions(`${PermOwner.Dashboard}@${PermAct.R}`)
3554
async salesTotal(@Rest() rest, @Loc() loc: Location, @Res() response) {
@@ -56,7 +75,7 @@ export class DashboardController {
5675
.where({ restaurant_id: rest.id })
5776
.andWhere('t1.status = :status', { status: OrderStatus.Completed })
5877
.groupBy('t1.created_at')
59-
.selectWithAlias(['_IFNULL(SUM(t1.net_total),0) AS net_total', 'created_at']);
78+
.selectWithAlias(['_IFNULL(SUM(t1.net_total), 0) AS net_total', 'created_at']);
6079

6180
if (loc) {
6281
query.andWhere({ location_id: loc.id });

src/app/staff/profile/profile.controller.ts

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export class StaffProfileController {
2222
@UseGuards(StaffGuard)
2323
@Permissions(`${PermStaff.Profile}@${PermAct.R}`)
2424
async me(@Me() me: StaffUser, @Res() response) {
25-
console.log(me);
2625
return response.item(me, StaffTransformer);
2726
}
2827

src/core/guards/owner.guard.ts

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export class OwnerGuard implements CanActivate {
7575
const request = context.switchToHttp().getRequest();
7676

7777
const permissions = this.reflector.get<string[]>('Permissions', context.getHandler());
78-
console.log({ permissions });
7978
if (!permissions) {
8079
throw new GuardException('Bad permission');
8180
}

src/core/guards/staff.guard.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Reflector } from '@nestjs/core';
1010
import { get } from 'lodash';
1111

1212
const validateRestaurant = async (request: any) => {
13-
console.log(request);
1413
const user: StaffUser = request.user;
1514
if (!user) {
1615
throw new TokenException('Getting user was failed');

src/database/seeds/dump/initial.sql

+21-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,26 @@ VALUES
1010
(
1111
'01EP4ZDWCZCHVFA1ZHV13YGRJZ',
1212
'owner',
13-
'[\"owner_role@read\", \"owner_role@create\", \"owner_role@update\", \"owner_role@delete\", \"owner_restaurant@read\", \"owner_restaurant@create\", \"owner_restaurant@update\", \"owner_restaurant@delete\", \"owner_profile@read\", \"owner_profile@create\", \"owner_profile@update\", \"owner_profile@delete\", \"owner_staff@read\", \"owner_staff@create\", \"owner_staff@update\", \"owner_staff@delete\", \"owner_location@read\", \"owner_location@create\", \"owner_location@update\", \"owner_location@delete\", \"owner_table@read\", \"owner_table@create\", \"owner_table@update\", \"owner_table@delete\", \"owner_category@read\", \"owner_category@create\", \"owner_category@update\", \"owner_category@delete\", \"owner_variant@read\", \"owner_variant@create\", \"owner_variant@update\", \"owner_variant@delete\", \"owner_product@read\", \"owner_product@create\", \"owner_product@update\", \"owner_product@delete\", \"owner_stock@read\", \"owner_stock@create\", \"owner_stock@update\", \"owner_stock@delete\", \"owner_order@read\", \"owner_order@create\", \"owner_order@update\", \"owner_order@delete\", \"owner_dashboard@read\", \"owner_dashboard@create\", \"owner_dashboard@update\", \"owner_dashboard@delete\"]'
14-
'2023-01-06 16:00:00.501148'
13+
'[\"owner_role@read\", \"owner_role@create\", \"owner_role@update\", \"owner_role@delete\", \"owner_restaurant@read\", \"owner_restaurant@create\", \"owner_restaurant@update\", \"owner_restaurant@delete\", \"owner_profile@read\", \"owner_profile@create\", \"owner_profile@update\", \"owner_profile@delete\", \"owner_staff@read\", \"owner_staff@create\", \"owner_staff@update\", \"owner_staff@delete\", \"owner_location@read\", \"owner_location@create\", \"owner_location@update\", \"owner_location@delete\", \"owner_table@read\", \"owner_table@create\", \"owner_table@update\", \"owner_table@delete\", \"owner_category@read\", \"owner_category@create\", \"owner_category@update\", \"owner_category@delete\", \"owner_variant@read\", \"owner_variant@create\", \"owner_variant@update\", \"owner_variant@delete\", \"owner_product@read\", \"owner_product@create\", \"owner_product@update\", \"owner_product@delete\", \"owner_stock@read\", \"owner_stock@create\", \"owner_stock@update\", \"owner_stock@delete\", \"owner_order@read\", \"owner_order@create\", \"owner_order@update\", \"owner_order@delete\", \"owner_dashboard@read\", \"owner_dashboard@create\", \"owner_dashboard@update\", \"owner_dashboard@delete\"]' '2023-01-06 16:00:00.501148'
14+
);
15+
16+
INSERT INTO
17+
`staff_role` (
18+
`id`,
19+
`created_at`,
20+
`updated_at`,
21+
`slug`,
22+
`permissions`,
23+
`is_default`
24+
)
25+
VALUES
26+
(
27+
'01J1QHZNYKXAFHTAX7WX1Z3K1W',
28+
'2024-07-01 23:41:25.000000',
29+
'2024-08-10 20:34:01.173032',
30+
'cashier',
31+
'[\"staff_role@read\", \"staff_restaurant@read\", \"staff_restaurant@create\", \"staff_restaurant@update\", \"staff_restaurant@delete\", \"staff_profile@read\", \"staff_profile@create\", \"staff_profile@update\", \"staff_profile@delete\", \"staff_staff@read\", \"staff_staff@update\", \"staff_location@read\", \"staff_location@create\", \"staff_location@update\", \"staff_location@delete\", \"staff_table@read\", \"staff_table@create\", \"staff_table@update\", \"staff_table@delete\", \"staff_category@read\", \"staff_category@create\", \"staff_category@update\", \"staff_category@delete\", \"staff_variant@read\", \"staff_variant@create\", \"staff_variant@update\", \"staff_variant@delete\", \"staff_product@read\", \"staff_product@create\", \"staff_product@update\", \"staff_product@delete\", \"staff_stock@read\", \"staff_stock@create\", \"staff_stock@update\", \"staff_stock@delete\", \"staff_order@read\", \"staff_order@create\", \"staff_order@update\", \"staff_order@delete\", \"staff_dashboard@read\", \"staff_dashboard@create\", \"staff_dashboard@update\", \"staff_dashboard@delete\"]',
32+
1
1533
);
1634

1735
--
@@ -35,7 +53,7 @@ VALUES
3553
(
3654
'01F2KFTXZNS01CJQCGNPJKXA1N',
3755
'Yudha',
38-
'owner@yuppey.com',
56+
'owner@shimy.com',
3957
'+6281931006841',
4058
'$2a$10$9/R3pR5rP1gnnG0n06pbEORY39fXfuJ2.eJkdqvoi5oDScm1gcHRi',
4159
NULL,

src/library/helpers/utils.helper.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export const getChartData = async (
257257
{ start, end }: { start: Date; end: Date },
258258
data: any[],
259259
columnName: string
260-
): Promise<{ id: string; count: number; total: number; label: string }[]> => {
260+
): Promise<{ id: string; count: number; total: number; label: string; labelType: 'daily' | 'hourly' }[]> => {
261261
start =
262262
start ||
263263
time(Math.min(...data.map((v) => v.created_at)))
@@ -279,12 +279,13 @@ export const getChartData = async (
279279
const groupedData = groupBy(data, (o) => time(o.created_at).format(formatTime));
280280
const label = timeCounter.format(formatTime);
281281
const total = groupedData[label]?.reduce((prevValue, data) => prevValue + Number(data[columnName]), 0) || 0;
282-
const otbCount = groupedData[label]?.length || 0;
282+
const dataCount = groupedData[label]?.length || 0;
283283
const result = {
284284
id: uuid(),
285-
count: otbCount,
285+
count: dataCount,
286286
total,
287-
label,
287+
label: timeCounter,
288+
labelType: timeDiffType,
288289
};
289290

290291
results.push(result);

0 commit comments

Comments
 (0)