tachybase_todo/packages/samples/shop-i18n/src/server/index.ts
chenos 249dff16d3
refactor: plugin manager (#965)
* feat: improve code

* chore: update version

* feat: api service

* fix: api services

* feat: improve code

* feat: improve code

* feat: improve code

* feat: pm socket

* fix: test errors

* feat: add built-in plugins before upgrade

* feat: update docs

* feat: improve code

* fix: after load
2022-10-27 13:00:16 +08:00

67 lines
1.5 KiB
TypeScript

import path from 'path';
import { InstallOptions, Plugin } from '@nocobase/server';
import zhCN from './locales/zh-CN';
const ns = '@nocobase/plugin-sample-shop-i18n';
export class ShopPlugin extends Plugin {
beforeLoad() {
// TODO
}
async load() {
await this.db.import({
directory: path.resolve(__dirname, 'collections'),
});
this.app.i18n.addResources('zh-CN', ns, zhCN);
this.app.resource({
name: 'orders',
actions: {
async create(ctx, next) {
const productRepo = ctx.db.getRepository('products');
const product = await productRepo.findById(ctx.action.params.values.productId);
if (!product) {
return ctx.throw(404, ctx.t('No such product', { ns }));
}
if (!product.enabled) {
return ctx.throw(400, ctx.t('Product not on sale', { ns }));
}
if (!product.inventory) {
return ctx.throw(400, ctx.t('Out of stock', { ns }));
}
const orderRepo = ctx.db.getRepository('orders');
ctx.body = await orderRepo.create({
values: {
productId: product.id,
quantity: 1,
totalPrice: product.price,
userId: ctx.state.currentUser.id
}
});
next();
}
}
});
this.app.acl.allow('products', '*');
this.app.acl.allow('categories', '*');
this.app.acl.allow('orders', '*');
}
async install(options: InstallOptions) {
// TODO
}
}
export default ShopPlugin;