Skip to content

Commit

Permalink
feat(com-api): admin user-list-inc-order route (#1189)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd authored May 27, 2023
2 parents 791793b + 4e936d6 commit 24ed6ac
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 360 deletions.
6 changes: 5 additions & 1 deletion core/type/src/customer-order-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const orderStatusCS = [
] as const;
export type OrderStatus = (typeof orderStatusCS)[number];

export const userPermissionsCS = ['user/patch', 'price/patch', 'product/patch', 'user-list/read'] as const;
export const userPermissionsCS = ['user/patch', 'price/patch', 'product/patch', 'user-list-inc-order/read'] as const;
export type UserPermission = (typeof userPermissionsCS)[number];

// -- Document object --
Expand Down Expand Up @@ -179,6 +179,10 @@ export interface ComUser extends User {
priceListName?: string;
}

export interface ComUserIncOrder extends ComUser {
orderList: Record<string, Order>;
}

// -- Schema --

export const orderInfoSchema = {
Expand Down
681 changes: 342 additions & 339 deletions uniquely/com-api/demo.http

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion uniquely/com-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {logger} from './lib/config.js';
import './root-user.js';
import './route/admin/user-list-inc-order.js';
import './route/home.js';
import './route/patch-price-list.js';
import './route/patch-product-list.js';
import './route/patch-user.js';
import './route/put-order.js';
import './route/user-list.js';

logger.logOther?.('..:: Alwatr Customer Order Management API ::..');
24 changes: 24 additions & 0 deletions uniquely/com-api/src/lib/user-order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {config} from './config.js';
import {storageClient} from './storage.js';

import type {AlwatrDocumentStorage} from '@alwatr/type';
import type {ComUserIncOrder, Order} from '@alwatr/type/src/customer-order-management.js';

export const getUserOrder = (userId: string): Promise<AlwatrDocumentStorage<Order>> => {
return storageClient.getStorage<Order>(config.privateStorage.userOrderList.replace('${userId}', userId));
};

export const patchUserOrder = (userId: string, order: Order): Promise<Order> => {
return storageClient.set<Order>(order, config.privateStorage.userOrderList.replace('${userId}', userId));
};

export const getUserListIncOrder = async (): Promise<Record<string, ComUserIncOrder>> => {
const userList = (await storageClient.getStorage<ComUserIncOrder>(config.privateStorage.userList)).data;

for (const user of Object.values(userList)) {
if (!Object.prototype.hasOwnProperty.call(userList, user.id)) continue;
user.orderList = (await getUserOrder(user.id)).data;
}

return userList;
};
17 changes: 17 additions & 0 deletions uniquely/com-api/src/route/admin/user-list-inc-order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {logger} from '../../lib/config.js';
import {nanoServer} from '../../lib/server.js';
import {getUserListIncOrder} from '../../lib/user-order.js';
import {validateUserAuth} from '../../lib/validate-user-auth.js';

import type {ComUser} from '@alwatr/type/customer-order-management.js';

nanoServer.route<Record<string, ComUser>>('GET', '/admin/user-list-inc-order', async (connection) => {
logger.logMethod?.('get-admin-user-list-inc-order');

await validateUserAuth(connection.getUserAuth(), 'user-list-inc-order/read');

return {
ok: true,
data: await getUserListIncOrder(),
};
});
6 changes: 3 additions & 3 deletions uniquely/com-api/src/route/put-order.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {config, logger} from '../lib/config.js';
import {logger} from '../lib/config.js';
import {nanoServer} from '../lib/server.js';
import {storageClient} from '../lib/storage.js';
import {patchUserOrder} from '../lib/user-order.js';
import {validateUserAuth} from '../lib/validate-user-auth.js';

import type {Order} from '@alwatr/type/customer-order-management.js';
Expand All @@ -20,7 +20,7 @@ nanoServer.route<Order>('PUT', '/order', async (connection) => {
order.clientId = clientId;
order.remoteAddress = remoteAddress;

order = await storageClient.set<Order>(order, config.privateStorage.userOrderList.replace('${userId}', userAuth.id));
order = await patchUserOrder(userAuth.id, order);

return {
ok: true,
Expand Down
16 changes: 0 additions & 16 deletions uniquely/com-api/src/route/user-list.ts

This file was deleted.

0 comments on commit 24ed6ac

Please sign in to comment.