fix: code format

This commit is contained in:
chenos 2023-01-03 15:17:20 +08:00
parent 9d618315ae
commit 0177f775fc
5 changed files with 39 additions and 40 deletions

View File

@ -1,7 +1,6 @@
import { InstallOptions, Plugin } from '@nocobase/server'; import { InstallOptions, Plugin } from '@nocobase/server';
export class HelloPlugin extends Plugin { export class HelloPlugin extends Plugin {
beforeLoad() { beforeLoad() {
// TODO // TODO
} }

View File

@ -3,7 +3,6 @@ import path from 'path';
import { InstallOptions, Plugin } from '@nocobase/server'; import { InstallOptions, Plugin } from '@nocobase/server';
export class ShopPlugin extends Plugin { export class ShopPlugin extends Plugin {
beforeLoad() { beforeLoad() {
// TODO // TODO
} }
@ -19,7 +18,7 @@ export class ShopPlugin extends Plugin {
create: { create: {
blacklist: ['id', 'totalPrice', 'status', 'createdAt', 'updatedAt'], blacklist: ['id', 'totalPrice', 'status', 'createdAt', 'updatedAt'],
values: { values: {
status: 0 status: 0,
}, },
middlewares: [ middlewares: [
async (ctx, next) => { async (ctx, next) => {
@ -30,9 +29,9 @@ export class ShopPlugin extends Plugin {
filter: { filter: {
enabled: true, enabled: true,
inventory: { inventory: {
$gt: 0 $gt: 0,
} },
} },
}); });
if (!product) { if (!product) {
@ -42,7 +41,7 @@ export class ShopPlugin extends Plugin {
ctx.state.product = product; ctx.state.product = product;
await next(); await next();
} },
], ],
async handler(ctx, next) { async handler(ctx, next) {
const { product } = ctx.state; const { product } = ctx.state;
@ -51,22 +50,22 @@ export class ShopPlugin extends Plugin {
...ctx.action.params.values, ...ctx.action.params.values,
productId: product.id, productId: product.id,
quantity: 1, quantity: 1,
totalPrice: product.price totalPrice: product.price,
} },
}); });
ctx.body = order; ctx.body = order;
} },
}, },
list: { list: {
filter: { filter: {
// 由 users 插件扩展的过滤器运算符 // 由 users 插件扩展的过滤器运算符
$isCurrentUser: true, $isCurrentUser: true,
status: { status: {
$ne: -1 $ne: -1,
} },
}, },
fields: ['id', 'status', 'createdAt', 'updatedAt'] fields: ['id', 'status', 'createdAt', 'updatedAt'],
}, },
async deliver(ctx, next) { async deliver(ctx, next) {
const { filterByTk } = ctx.action.params; const { filterByTk } = ctx.action.params;
@ -78,17 +77,17 @@ export class ShopPlugin extends Plugin {
status: 2, status: 2,
delivery: { delivery: {
...ctx.action.params.values, ...ctx.action.params.values,
status: 0 status: 0,
} },
} },
}); });
order.delivery = await order.getDelivery(); order.delivery = await order.getDelivery();
ctx.body = order; ctx.body = order;
next(); next();
} },
} },
}); });
this.app.acl.allow('products', '*'); this.app.acl.allow('products', '*');

View File

@ -14,27 +14,27 @@ export class ShopPlugin extends Plugin {
filter: { filter: {
status: 0, status: 0,
createdAt: { createdAt: {
$lt: expiredDate $lt: expiredDate,
} },
} },
}); });
await deliveryRepo.update({ await deliveryRepo.update({
filter: { filter: {
id: expiredDeliveries.map(item => item.get('id')), id: expiredDeliveries.map((item) => item.get('id')),
}, },
values: { values: {
status: 1 status: 1,
} },
}); });
const orderRepo = this.db.getRepository('orders'); const orderRepo = this.db.getRepository('orders');
const [updated] = await orderRepo.update({ const [updated] = await orderRepo.update({
filter: { filter: {
status: 2, status: 2,
id: expiredDeliveries.map(item => item.get('orderId')) id: expiredDeliveries.map((item) => item.get('orderId')),
}, },
values: { values: {
status: 3 status: 3,
} },
}); });
console.log('%d orders expired', updated); console.log('%d orders expired', updated);
@ -51,14 +51,17 @@ export class ShopPlugin extends Plugin {
this.db.on('orders.afterCreate', async (order, options) => { this.db.on('orders.afterCreate', async (order, options) => {
const product = await order.getProduct({ const product = await order.getProduct({
transaction: options.transaction transaction: options.transaction,
}); });
await product.update({ await product.update(
inventory: product.inventory - order.quantity {
}, { inventory: product.inventory - order.quantity,
transaction: options.transaction },
}); {
transaction: options.transaction,
},
);
}); });
this.app.on('beforeStart', () => { this.app.on('beforeStart', () => {

View File

@ -7,7 +7,6 @@ import zhCN from './locales/zh-CN';
const ns = '@nocobase/plugin-sample-shop-i18n'; const ns = '@nocobase/plugin-sample-shop-i18n';
export class ShopPlugin extends Plugin { export class ShopPlugin extends Plugin {
beforeLoad() { beforeLoad() {
// TODO // TODO
} }
@ -44,13 +43,13 @@ export class ShopPlugin extends Plugin {
productId: product.id, productId: product.id,
quantity: 1, quantity: 1,
totalPrice: product.price, totalPrice: product.price,
userId: ctx.state.currentUser.id userId: ctx.state.currentUser.id,
} },
}); });
next(); next();
} },
} },
}); });
this.app.acl.allow('products', '*'); this.app.acl.allow('products', '*');

View File

@ -5,10 +5,9 @@ import { InstallOptions, Plugin } from '@nocobase/server';
import SnowflakeField from './fields/SnowflakeField'; import SnowflakeField from './fields/SnowflakeField';
export class ShopPlugin extends Plugin { export class ShopPlugin extends Plugin {
afterAdd() { afterAdd() {
this.db.registerFieldTypes({ this.db.registerFieldTypes({
snowflake: SnowflakeField snowflake: SnowflakeField,
}); });
} }