Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Commit

Permalink
(update) dockerfiles and products service
Browse files Browse the repository at this point in the history
  • Loading branch information
sckv committed Apr 26, 2020
1 parent ffa9bc4 commit f7787aa
Show file tree
Hide file tree
Showing 71 changed files with 3,019 additions and 102 deletions.
1 change: 0 additions & 1 deletion _dbscript/db.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
CREATE DATABASE accounting;
CREATE DATABASE products;
CREATE DATABASE ordering;
CREATE DATABASE common;
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
build: ./packages/accounts
container_name: accounts
ports:
- '3000:3000'
- '3003:3000'
networks:
- local
volumes:
Expand Down Expand Up @@ -68,7 +68,7 @@ services:
build: ./packages/graphql
container_name: graphql
ports:
- '3003:3000'
- '3000:3000'
networks:
- local
volumes:
Expand Down
5 changes: 0 additions & 5 deletions packages/accounting/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@ RUN apk update
RUN apk add curl bash git

WORKDIR /usr/src/app

COPY . .

RUN npm install --production
USER node
Empty file.
7 changes: 7 additions & 0 deletions packages/accounting/bootstrap/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// import { ex } from '~external/express';
// import { retrieveInvoiceController } from './bootstrap';

// ASSOC ROUTES
// ex.route('invoices/:email').get(retrieveInvoiceController.handle);

console.log('Express routes bootstrapped!');
13 changes: 13 additions & 0 deletions packages/accounting/bootstrap/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// import { nats } from '~external/nats';
// import { Client } from 'ts-nats';
// import { createInvoiceHandler, payInvoiceHandler } from './bootstrap';

// const bootstrapNats = async () => {
// const client = (await nats) as Client;

// ASSOC LISTENERS
// client.subscribe(process.env.INVOICE_CREATE!, createInvoiceHandler.handle);
// client.subscribe(process.env.INVOICE_PAY!, payInvoiceHandler.handle);
// };

// bootstrapNats().then(() => console.log('Nats listeners bootstrapped!'));
2 changes: 2 additions & 0 deletions packages/accounting/bootstrap/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './express';
import './queue';
3 changes: 0 additions & 3 deletions packages/accounting/db/migrate.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import knex from 'knex';
import { options } from './knexfile';

export const connection = knex(options);
export const database = knex(options);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const options = {
client: 'pg',
client: 'postgres',
connection: {
database: 'accounting',
host: process.env.DB_IP,
Expand Down
6 changes: 6 additions & 0 deletions packages/accounting/external/db/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { database } from './index';

database.migrate
.latest()
.then(() => console.log('Migrated successfully'))
.catch((err) => console.error('Error during migration', err));
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
exports.up = async (db) => {
await db.schema.createTable('accounts', (t) => {
t.uuid('id').primary();
t.string('email');
t.string('email').primary();
t.string('password');
t.decimal('balance');
t.timestamp('created_at').defaultTo(db.fn.now());
});
};

exports.down = async (db) => db.schema.dropTable('accounts')
exports.down = async (db) => db.schema.dropTable('accounts');
21 changes: 21 additions & 0 deletions packages/accounting/external/express/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express from 'express';
import morgan from 'morgan';
import compression from 'compression';
import bodyParser from 'body-parser';
import { Router } from 'express';

export const ex = Router();

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(compression());
app.use(morgan('combined'));

// ROUTER
app.use(ex);

app.listen(3000, () => {
console.log('Express server online!');
});
20 changes: 20 additions & 0 deletions packages/accounting/external/http/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import got from 'got';

export class Fetcher {
constructor() {
Object.freeze(this);
}

static async get(url: string, searchParams?: { [key: string]: number | string }) {
return got.get(url, { searchParams });
}
static async post(url: string, body: { [key: string]: number | string } = {}) {
return got.post(url, { body: JSON.stringify(body) });
}
static async delete(url: string, searchParams?: { [key: string]: number | string }) {
return got.delete(url, { searchParams });
}
static async patch(url: string, body: { [key: string]: number | string } = {}) {
return got.patch(url, { body: JSON.stringify(body) });
}
}
7 changes: 7 additions & 0 deletions packages/accounting/external/nats/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { connect, Payload } from 'ts-nats';

export const nats = connect({
payload: Payload.JSON,
url: process.env.NATS_URL,
}).then((client) => client);
// .catch((err) => console.error('NATS connection error', err));
19 changes: 0 additions & 19 deletions packages/accounting/knexfile.js

This file was deleted.

27 changes: 24 additions & 3 deletions packages/accounting/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
{
"name": "accounts",
"name": "accounting",
"version": "1.0.0",
"main": "index.js",
"main": "app/entrypoint.ts",
"license": "MIT",
"scripts": {
"start": "ts-node-dev --files -r tsconfig-paths/register app/entrypoint.ts"
},
"dependencies": {
"ts-node-dev": "^1.0.0-pre.44"
"@types/compression": "^1.7.0",
"@types/express": "^4.17.6",
"@types/morgan": "^1.9.0",
"@types/node": "^13.13.2",
"@types/uuid": "^7.0.3",
"@types/validator": "^13.0.0",
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"express": "^4.17.1",
"got": "^11.0.2",
"knex": "^0.21.0",
"morgan": "^1.10.0",
"pg": "^8.0.3",
"ts-nats": "^1.2.12",
"ts-node": "^8.9.1",
"ts-node-dev": "^1.0.0-pre.44",
"tsconfig-paths": "^3.9.0",
"uuid": "^7.0.3",
"validator": "^13.0.0"
}
}
15 changes: 11 additions & 4 deletions packages/accounting/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"target": "es2019",
"sourceMap": true,
"module": "CommonJS",
"outDir": "lib",
"moduleResolution": "node",
"lib": ["dom", "es6", "es2016", "es2017.object", "es2015.promise"],
"allowJs": false,
"alwaysStrict": true,
"charset": "UTF-8",
"isolatedModules": false,
"keyofStringsOnly": false,
"skipLibCheck": true,
"esModuleInterop": true,
Expand All @@ -34,8 +34,15 @@
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"allowSyntheticDefaultImports": true,
"rootDirs": ["app/**/*"],
"incremental": false
"rootDir": ".",
"sourceRoot": ".",
"baseUrl": ".",
"incremental": false,
"paths": {
"~app/*": ["./app/*"],
"~external/*": ["./external/*"],
"~bootstrap/*": ["./bootstrap/*"]
}
},
"include": ["app/**/*"]
"include": ["app", "external", "bootstrap"]
}
5 changes: 0 additions & 5 deletions packages/billing/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@ RUN apk update
RUN apk add curl bash git

WORKDIR /usr/src/app

COPY . .

RUN npm install --production
USER node
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ export class CreateInvoiceCommand {
}

const balanceCharge = await this.services.balance.paymentCharge(this.invoice);
if (!balanceCharge.isCharged) {
if (!balanceCharge.isEmitted) {
return {
isError: true,
message: 'Invoice balance charge is failed it is not charged',
message: 'Invoice balance charge emission failed it is not charged',
};
}

const invoiceCharge = await this.services.invoice.chargeInvoice(this.invoice.id);
const invoiceCharge = await this.services.invoice.setInvoiceCharged(this.invoice.id);
if (!invoiceCharge.isCharged)
return {
isError: true,
message: 'Invoice charge is failed in the database, although it is charged correctly in the third party',
message:
'Invoice charge flag change is failed in the database, although Invoce been charged correctly in the third party',
};

return { invoiceCreated: true };
Expand Down
13 changes: 13 additions & 0 deletions packages/billing/app/domain/entities/Invoice.ent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,17 @@ export class Invoice {
this.invoice.paid = false;
return this;
}

toJSON() {
return {
id: this.id,
tax: this.tax,
paid: this.paid,
charged: this.charged,
address: this.address,
price: this.price,
orderId: this.orderId,
email: this.email,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { Client } from 'ts-nats';

export abstract class BalanceRelatedService {
constructor(protected nats: Promise<Client>) {}
abstract async paymentCharge(invoice: Invoice): Promise<{ isCharged: boolean } | Error>;
abstract async paymentCharge(invoice: Invoice): Promise<{ isEmitted: boolean } | Error>;
}
2 changes: 1 addition & 1 deletion packages/billing/app/domain/services/Invoicing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export abstract class InvoicingService {
constructor(protected db: Knex) {}
abstract async createInvoice(invoice: Invoice): Promise<{ isCreated: boolean } | Error>;
abstract async payInvoice(invoiceId: string): Promise<{ isPaid: boolean } | Error>;
abstract async chargeInvoice(invoiceId: string): Promise<{ isCharged: boolean } | Error>;
abstract async setInvoiceCharged(invoiceId: string): Promise<{ isCharged: boolean } | Error>;
abstract async retrieveInvoices(email: Email): Promise<Invoice[] | Error>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export class BalanceRelatedServiceRepository extends BalanceRelatedService {
price: invoice.totalPrice,
});

return { isCharged: true };
return { isEmitted: true };
}
}
4 changes: 2 additions & 2 deletions packages/billing/app/infrastructure/Invoicing.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class InvoicingServiceRepository extends InvoicingService {
return { isCreated: true };
}

async chargeInvoice(invoiceId: string) {
async setInvoiceCharged(invoiceId: string) {
await this.db.table('invoice').where({ id: invoiceId }).update({
charged: true,
});
Expand All @@ -37,7 +37,7 @@ export class InvoicingServiceRepository extends InvoicingService {
}

async retrieveInvoices(email: Email) {
const invoices = await this.db.table('invoice').select('*').where({ email: email.value });
const invoices = await this.db.table('invoice').select().where({ email: email.value });

return invoices.map(
({ id, tax, price, order_id: orderId, email, address, paid, charged }) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/billing/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "billing",
"version": "1.0.0",
"main": "index.js",
"main": "app/entrypoint.ts",
"license": "MIT",
"scripts": {
"start": "ts-node-dev --files -r tsconfig-paths/register app/entrypoint.ts"
Expand Down
5 changes: 0 additions & 5 deletions packages/graphql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@ RUN apk update
RUN apk add curl bash git

WORKDIR /usr/src/app

COPY . .

RUN npm install --production
USER node
2 changes: 2 additions & 0 deletions packages/graphql/src/schema/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Order.addResolver({
name: 'placeOrder',
type: () => Order,
args: {
address: 'String!',
billingAddress: 'String',
products: {
type: () => [ProductInput],
},
Expand Down
7 changes: 1 addition & 6 deletions packages/ordering/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@ LABEL name="node12:local"
RUN apk update
RUN apk add curl bash git

WORKDIR /usr/src/app

COPY . .

RUN npm install --production
USER node
WORKDIR /usr/src/app
Empty file.
7 changes: 7 additions & 0 deletions packages/ordering/bootstrap/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// import { ex } from '~external/express';
// import { retrieveInvoiceController } from './bootstrap';

// ASSOC ROUTES
// ex.route('invoices/:email').get(retrieveInvoiceController.handle);

console.log('Express routes bootstrapped!');
13 changes: 13 additions & 0 deletions packages/ordering/bootstrap/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// import { nats } from '~external/nats';
// import { Client } from 'ts-nats';
// import { createInvoiceHandler, payInvoiceHandler } from './bootstrap';

// const bootstrapNats = async () => {
// const client = (await nats) as Client;

// ASSOC LISTENERS
// client.subscribe(process.env.INVOICE_CREATE!, createInvoiceHandler.handle);
// client.subscribe(process.env.INVOICE_PAY!, payInvoiceHandler.handle);
// };

// bootstrapNats().then(() => console.log('Nats listeners bootstrapped!'));
2 changes: 2 additions & 0 deletions packages/ordering/bootstrap/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './express';
import './queue';
3 changes: 0 additions & 3 deletions packages/ordering/db/migrate.ts

This file was deleted.

Loading

0 comments on commit f7787aa

Please sign in to comment.