("delete-product", {
95 | id: productId,
96 | user_id: id
97 | })
98 | .subscribe(
99 | product => {
100 | redis.del(redisProductsKey);
101 | return resolve(product);
102 | },
103 | error => reject(error)
104 | );
105 | });
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/server/services/gateway/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
6 | [travis-url]: https://travis-ci.org/nestjs/nest
7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
8 | [linux-url]: https://travis-ci.org/nestjs/nest
9 |
10 | A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
26 |
27 | ## Description
28 |
29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
30 |
31 | ## Installation
32 |
33 | ```bash
34 | $ npm install
35 | ```
36 |
37 | ## Running the app
38 |
39 | ```bash
40 | # development
41 | $ npm run start
42 |
43 | # watch mode
44 | $ npm run start:dev
45 |
46 | # production mode
47 | $ npm run start:prod
48 | ```
49 |
50 | ## Test
51 |
52 | ```bash
53 | # unit tests
54 | $ npm run test
55 |
56 | # e2e tests
57 | $ npm run test:e2e
58 |
59 | # test coverage
60 | $ npm run test:cov
61 | ```
62 |
63 | ## Support
64 |
65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
66 |
67 | ## Stay in touch
68 |
69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
70 | - Website - [https://nestjs.com](https://nestjs.com/)
71 | - Twitter - [@nestframework](https://twitter.com/nestframework)
72 |
73 | ## License
74 |
75 | Nest is [MIT licensed](LICENSE).
76 |
--------------------------------------------------------------------------------
/server/services/orders/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
6 | [travis-url]: https://travis-ci.org/nestjs/nest
7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
8 | [linux-url]: https://travis-ci.org/nestjs/nest
9 |
10 | A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
26 |
27 | ## Description
28 |
29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
30 |
31 | ## Installation
32 |
33 | ```bash
34 | $ npm install
35 | ```
36 |
37 | ## Running the app
38 |
39 | ```bash
40 | # development
41 | $ npm run start
42 |
43 | # watch mode
44 | $ npm run start:dev
45 |
46 | # production mode
47 | $ npm run start:prod
48 | ```
49 |
50 | ## Test
51 |
52 | ```bash
53 | # unit tests
54 | $ npm run test
55 |
56 | # e2e tests
57 | $ npm run test:e2e
58 |
59 | # test coverage
60 | $ npm run test:cov
61 | ```
62 |
63 | ## Support
64 |
65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
66 |
67 | ## Stay in touch
68 |
69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
70 | - Website - [https://nestjs.com](https://nestjs.com/)
71 | - Twitter - [@nestframework](https://twitter.com/nestframework)
72 |
73 | ## License
74 |
75 | Nest is [MIT licensed](LICENSE).
76 |
--------------------------------------------------------------------------------
/server/services/users/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
6 | [travis-url]: https://travis-ci.org/nestjs/nest
7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
8 | [linux-url]: https://travis-ci.org/nestjs/nest
9 |
10 | A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
26 |
27 | ## Description
28 |
29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
30 |
31 | ## Installation
32 |
33 | ```bash
34 | $ npm install
35 | ```
36 |
37 | ## Running the app
38 |
39 | ```bash
40 | # development
41 | $ npm run start
42 |
43 | # watch mode
44 | $ npm run start:dev
45 |
46 | # production mode
47 | $ npm run start:prod
48 | ```
49 |
50 | ## Test
51 |
52 | ```bash
53 | # unit tests
54 | $ npm run test
55 |
56 | # e2e tests
57 | $ npm run test:e2e
58 |
59 | # test coverage
60 | $ npm run test:cov
61 | ```
62 |
63 | ## Support
64 |
65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
66 |
67 | ## Stay in touch
68 |
69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
70 | - Website - [https://nestjs.com](https://nestjs.com/)
71 | - Twitter - [@nestframework](https://twitter.com/nestframework)
72 |
73 | ## License
74 |
75 | Nest is [MIT licensed](LICENSE).
76 |
--------------------------------------------------------------------------------
/server/services/payments/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
6 | [travis-url]: https://travis-ci.org/nestjs/nest
7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
8 | [linux-url]: https://travis-ci.org/nestjs/nest
9 |
10 | A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
26 |
27 | ## Description
28 |
29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
30 |
31 | ## Installation
32 |
33 | ```bash
34 | $ npm install
35 | ```
36 |
37 | ## Running the app
38 |
39 | ```bash
40 | # development
41 | $ npm run start
42 |
43 | # watch mode
44 | $ npm run start:dev
45 |
46 | # production mode
47 | $ npm run start:prod
48 | ```
49 |
50 | ## Test
51 |
52 | ```bash
53 | # unit tests
54 | $ npm run test
55 |
56 | # e2e tests
57 | $ npm run test:e2e
58 |
59 | # test coverage
60 | $ npm run test:cov
61 | ```
62 |
63 | ## Support
64 |
65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
66 |
67 | ## Stay in touch
68 |
69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
70 | - Website - [https://nestjs.com](https://nestjs.com/)
71 | - Twitter - [@nestframework](https://twitter.com/nestframework)
72 |
73 | ## License
74 |
75 | Nest is [MIT licensed](LICENSE).
76 |
--------------------------------------------------------------------------------
/server/services/products/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
6 | [travis-url]: https://travis-ci.org/nestjs/nest
7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
8 | [linux-url]: https://travis-ci.org/nestjs/nest
9 |
10 | A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
26 |
27 | ## Description
28 |
29 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
30 |
31 | ## Installation
32 |
33 | ```bash
34 | $ npm install
35 | ```
36 |
37 | ## Running the app
38 |
39 | ```bash
40 | # development
41 | $ npm run start
42 |
43 | # watch mode
44 | $ npm run start:dev
45 |
46 | # production mode
47 | $ npm run start:prod
48 | ```
49 |
50 | ## Test
51 |
52 | ```bash
53 | # unit tests
54 | $ npm run test
55 |
56 | # e2e tests
57 | $ npm run test:e2e
58 |
59 | # test coverage
60 | $ npm run test:cov
61 | ```
62 |
63 | ## Support
64 |
65 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
66 |
67 | ## Stay in touch
68 |
69 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
70 | - Website - [https://nestjs.com](https://nestjs.com/)
71 | - Twitter - [@nestframework](https://twitter.com/nestframework)
72 |
73 | ## License
74 |
75 | Nest is [MIT licensed](LICENSE).
76 |
--------------------------------------------------------------------------------
/server/services/gateway/src/orders/order.resolver.ts:
--------------------------------------------------------------------------------
1 | import { Client, ClientProxy, Transport } from "@nestjs/microservices";
2 | import {
3 | Resolver,
4 | Context,
5 | Mutation,
6 | Args,
7 | Query,
8 | ResolveProperty,
9 | Parent
10 | } from "@nestjs/graphql";
11 | import { UseGuards } from "@nestjs/common";
12 | import { config, ProductDTO, OrderDTO, UserDTO } from "@commerce/shared";
13 |
14 | import { AuthGuard } from "../middlewares/auth.guard";
15 | import { CreateOrder } from "./create-order.validation";
16 | import { OrderProductDataLoader } from "../loaders/order-product.loader";
17 | import { OrderService } from "./order.service";
18 | import { UUID } from "../shared/validation/uuid.validation";
19 | import { UserDataLoader } from "../loaders/user.loader";
20 |
21 | @Resolver("Order")
22 | export class OrderResolver {
23 | @Client({
24 | transport: Transport.REDIS,
25 | options: {
26 | url: `redis://${config.REDIS_URL}:${config.REDIS_PORT}`
27 | }
28 | })
29 | private client: ClientProxy;
30 |
31 | constructor(
32 | private readonly orderService: OrderService,
33 | private readonly usersDataLoader: UserDataLoader,
34 | private readonly orderProductLoader: OrderProductDataLoader
35 | ) {}
36 | @ResolveProperty("user", () => UserDTO)
37 | async user(@Parent() order: OrderDTO): Promise {
38 | return this.usersDataLoader.load(order.user_id);
39 | }
40 | @ResolveProperty("products", () => ProductDTO)
41 | async products(@Parent() order): Promise {
42 | return this.orderProductLoader.loadMany(order.products);
43 | }
44 | @Query()
45 | @UseGuards(new AuthGuard())
46 | orders(@Context("user") user: any): Promise {
47 | return this.orderService.indexOrdersByUser(user.id);
48 | }
49 | @Mutation()
50 | @UseGuards(new AuthGuard())
51 | deleteOrder(@Args("order") { id }: UUID, @Context("user") user: any) {
52 | return this.orderService.destroyUserOrder(id, user.id);
53 | }
54 | @Mutation()
55 | @UseGuards(new AuthGuard())
56 | createOrder(
57 | @Args("products") products: CreateOrder[],
58 | @Context("user") user: any
59 | ): Promise {
60 | return new Promise((resolve, reject) => {
61 | // fetch products user is trying to purchase to check on the quantity.
62 | this.client
63 | .send(
64 | "fetch-products-by-ids",
65 | products.map(product => product.id)
66 | )
67 | .subscribe(
68 | async fetchedProducts => {
69 | const filteredProducts = products.filter(product => {
70 | const p = fetchedProducts.find(
71 | p => p.id === product.id
72 | );
73 | return p.quantity >= product.quantity;
74 | });
75 | // there is something wrong with the quantity of passed products.
76 | if (filteredProducts.length != products.length) {
77 | return reject(
78 | "Products are out of stock at the moment, try with lower stock."
79 | );
80 | }
81 | return resolve(
82 | await this.orderService.store(
83 | products,
84 | user.id,
85 | fetchedProducts
86 | )
87 | );
88 | },
89 | error => reject(error)
90 | );
91 | });
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/server/services/payments/src/payments/payment.service.ts:
--------------------------------------------------------------------------------
1 | import { InjectRepository } from "@nestjs/typeorm";
2 | import { Injectable, NotFoundException } from "@nestjs/common";
3 | import { Repository } from "typeorm";
4 | import {
5 | RpcException,
6 | Client,
7 | Transport,
8 | ClientProxy
9 | } from "@nestjs/microservices";
10 | import { PaymentCardDTO, config } from "@commerce/shared";
11 |
12 | import { PaymentEntity } from "./payment.entity";
13 | import { stripe } from "../utils/stripe";
14 |
15 | @Injectable()
16 | export class PaymentService {
17 | @Client({
18 | transport: Transport.REDIS,
19 | options: {
20 | url: `redis://${config.REDIS_URL}:${config.REDIS_PORT}`
21 | }
22 | })
23 | private client: ClientProxy;
24 |
25 | constructor(
26 | @InjectRepository(PaymentEntity)
27 | private readonly payments: Repository
28 | ) {}
29 | async findByUserId(user_id) {
30 | return this.payments.findOneOrFail({ where: { user_id } });
31 | }
32 | async store(
33 | data: {
34 | token_id: string;
35 | last_four: string;
36 | provider_id: string;
37 | brand: string;
38 | default: boolean;
39 | },
40 | user
41 | ): Promise {
42 | // if user sets the payment card we are about to store to default, set others to false.
43 | if (data.default) {
44 | await this.payments.update(
45 | {
46 | user_id: user.id
47 | },
48 | { default: false }
49 | );
50 | }
51 | const paymentData = {
52 | last_four: data.last_four,
53 | provider_id: data.provider_id,
54 | default: data.default,
55 | brad: data.brand,
56 | user_id: user.id
57 | };
58 | // store the payment method to our database alongside with token.
59 | const payment = await this.payments.save(paymentData);
60 |
61 | // check if user has gateway_customer_id, if user doesn't have one, create one using stripe.
62 | if (!user.gateway_customer_id) {
63 | let address = {
64 | line1: user.address.address_1,
65 | line2: user.address.address_2,
66 | city: user.address.city,
67 | state: user.address.state,
68 | country: user.address.country,
69 | postal_code: user.address.zip
70 | };
71 | // fetch user addresses, email, provider_id, name and create a customer.
72 | const customer = await stripe.customers.create({
73 | email: user.email,
74 | name: user.name,
75 | source: data.token_id,
76 | address
77 | });
78 | // attach customer id to the user.gateway_customer_id
79 | this.client
80 | .emit("customer_created", {
81 | user_id: user.id,
82 | gateway_customer_id: customer.id
83 | })
84 | .subscribe(() => {});
85 | }
86 | delete payment.user_id;
87 | // @ts-ignore
88 | payment.user = user;
89 | return payment;
90 | }
91 | async get(user_id: string): Promise {
92 | return this.payments.find({ user_id });
93 | }
94 | async show(id: string, user_id: string): Promise {
95 | return this.payments.findOneOrFail({ id, user_id });
96 | }
97 | async destroy(id: string, user_id: string): Promise {
98 | const paymentCard = await this.payments.findOneOrFail({ id });
99 | if (paymentCard.user_id === user_id) {
100 | await this.payments.delete({ id });
101 | return paymentCard;
102 | }
103 | throw new RpcException(
104 | new NotFoundException("You cannot update what you don't own...")
105 | );
106 | }
107 | }
108 |
--------------------------------------------------------------------------------