|
|
@@ -1,8 +1,15 @@
|
|
|
import { OnApplicationBootstrap } from '@nestjs/common';
|
|
|
+import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
|
|
|
import {
|
|
|
Asset,
|
|
|
+ Channel,
|
|
|
+ CustomOrderFields,
|
|
|
+ CustomProductFields,
|
|
|
+ Order,
|
|
|
+ OrderService,
|
|
|
PluginCommonModule,
|
|
|
Product,
|
|
|
+ RequestContext,
|
|
|
TransactionalConnection,
|
|
|
User,
|
|
|
VendurePlugin,
|
|
|
@@ -12,6 +19,15 @@ import gql from 'graphql-tag';
|
|
|
import { ProfileAsset } from './profile-asset.entity';
|
|
|
import { Profile } from './profile.entity';
|
|
|
|
|
|
+declare module '@vendure/core' {
|
|
|
+ interface CustomOrderFields {
|
|
|
+ productOwner: User;
|
|
|
+ }
|
|
|
+ interface CustomProductFields {
|
|
|
+ owner: User;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
const schema = gql`
|
|
|
type Profile implements Node {
|
|
|
id: ID!
|
|
|
@@ -52,6 +68,16 @@ const schema = gql`
|
|
|
shopApiExtensions: { schema, resolvers: [] },
|
|
|
adminApiExtensions: { schema, resolvers: [] },
|
|
|
configuration: config => {
|
|
|
+ // Order
|
|
|
+ config.customFields.Order.push({
|
|
|
+ name: 'productOwner', // because orders are always c2c (and should be stored redundant in case product get's deleted)
|
|
|
+ nullable: true,
|
|
|
+ type: 'relation',
|
|
|
+ entity: User,
|
|
|
+ public: false,
|
|
|
+ eager: true,
|
|
|
+ readonly: true,
|
|
|
+ });
|
|
|
config.customFields.Product.push({
|
|
|
name: 'owner',
|
|
|
nullable: true,
|
|
|
@@ -76,9 +102,14 @@ const schema = gql`
|
|
|
},
|
|
|
})
|
|
|
export class Test1664Plugin implements OnApplicationBootstrap {
|
|
|
- constructor(private connection: TransactionalConnection) {}
|
|
|
+ constructor(private connection: TransactionalConnection, private orderService: OrderService) {}
|
|
|
|
|
|
async onApplicationBootstrap() {
|
|
|
+ await this.createDummyProfiles();
|
|
|
+ await this.createDummyOrder();
|
|
|
+ }
|
|
|
+
|
|
|
+ async createDummyProfiles() {
|
|
|
const profilesCount = await this.connection.rawConnection.getRepository(Profile).count();
|
|
|
if (0 < profilesCount) {
|
|
|
return;
|
|
|
@@ -111,4 +142,46 @@ export class Test1664Plugin implements OnApplicationBootstrap {
|
|
|
await this.connection.rawConnection.getRepository(Product).save(product);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ async createDummyOrder() {
|
|
|
+ const orderCount = await this.connection.rawConnection.getRepository(Order).count();
|
|
|
+ if (0 < orderCount) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const defaultChannel = await this.connection.getRepository(Channel).findOne({
|
|
|
+ relations: ['defaultShippingZone', 'defaultTaxZone'],
|
|
|
+ where: {
|
|
|
+ code: DEFAULT_CHANNEL_CODE,
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!defaultChannel) {
|
|
|
+ throw new Error(`Channel with code ${DEFAULT_CHANNEL_CODE} could not be found.`);
|
|
|
+ }
|
|
|
+
|
|
|
+ const ctx = new RequestContext({
|
|
|
+ apiType: 'shop',
|
|
|
+ authorizedAsOwnerOnly: false,
|
|
|
+ channel: defaultChannel,
|
|
|
+ isAuthorized: true,
|
|
|
+ languageCode: defaultChannel.defaultLanguageCode,
|
|
|
+ });
|
|
|
+
|
|
|
+ // Create order
|
|
|
+ const users = await this.connection.rawConnection.getRepository(User).find();
|
|
|
+ // tslint:disable-next-line:no-non-null-assertion
|
|
|
+ const customer = users[1]!;
|
|
|
+ const created = await this.orderService.create(ctx, customer.id);
|
|
|
+
|
|
|
+ // Add products
|
|
|
+ const products = await this.connection.rawConnection
|
|
|
+ .getRepository(Product)
|
|
|
+ .find({ relations: ['variants'] });
|
|
|
+ const product = products[0];
|
|
|
+ await this.orderService.addItemToOrder(ctx, created.id, product.variants[0].id, 1);
|
|
|
+ // Add the product owner to order
|
|
|
+ const productOwner = product.customFields.owner;
|
|
|
+ await this.orderService.updateCustomFields(ctx, created.id, { productOwner });
|
|
|
+ }
|
|
|
}
|