Basic database structure
- Change license
This commit is contained in:
56
src/app.ts
56
src/app.ts
@@ -1,15 +1,67 @@
|
||||
import * as rpc from 'jayson';
|
||||
import { createConnection } from 'typeorm';
|
||||
import * as winston from 'winston';
|
||||
|
||||
export const IS_DEBUG = process.env.DEBUG == 'true';
|
||||
|
||||
export let logger: winston.Logger;
|
||||
|
||||
async function run() {
|
||||
const { combine, timestamp, label, printf, prettyPrint } = winston.format;
|
||||
|
||||
const myFormat = printf(({ level, message, label, timestamp }) => {
|
||||
return `${timestamp} ${level} ${message}`;
|
||||
});
|
||||
|
||||
logger = winston.createLogger({
|
||||
level: IS_DEBUG ? 'debug' : 'info',
|
||||
levels: winston.config.syslog.levels,
|
||||
format: combine(
|
||||
timestamp(),
|
||||
prettyPrint(),
|
||||
myFormat
|
||||
),
|
||||
defaultMeta: { },
|
||||
transports: [
|
||||
new winston.transports.File({ filename: 'error.log', level: 'error' }),
|
||||
new winston.transports.File({ filename: 'combined.log' })
|
||||
]
|
||||
});
|
||||
|
||||
// Adding seperate logger for files (with color)
|
||||
logger.add(new winston.transports.Console({
|
||||
format: combine(
|
||||
winston.format.colorize({ level: true }),
|
||||
timestamp(),
|
||||
prettyPrint(),
|
||||
myFormat
|
||||
)
|
||||
}));
|
||||
|
||||
const dbConnection = await createConnection({
|
||||
type: 'mysql',
|
||||
host: 'localhost',
|
||||
port: 3306,
|
||||
username: 'librepay',
|
||||
password: 'librepay',
|
||||
database: 'librepay',
|
||||
entities: ['models/**/*.ts'],
|
||||
synchronize: true,
|
||||
logging: false
|
||||
}).catch(error => {
|
||||
logger.error(`Connection to database failed: ${error}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const client = rpc.Client.http({
|
||||
port: 18332,
|
||||
auth: 'admin:admin'
|
||||
});
|
||||
|
||||
client.request('getnewaddress', ['TestRPC', 'bech32'], (err, response) => {
|
||||
/*client.request('getnewaddress', ['TestRPC', 'bech32'], (err, response) => {
|
||||
if (err) throw err;
|
||||
console.log(response.result);
|
||||
})
|
||||
})*/
|
||||
}
|
||||
|
||||
run();
|
||||
5
src/controllers/invoice.ts
Normal file
5
src/controllers/invoice.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
export async function createInvoice(req: Request, res: Response) {
|
||||
const paymentMethods = req.body.methods;
|
||||
}
|
||||
0
src/helper/token.ts
Normal file
0
src/helper/token.ts
Normal file
36
src/helper/types.ts
Normal file
36
src/helper/types.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
export enum CryptoUnits {
|
||||
BITCOIN = 'BTC',
|
||||
ETHEREUM = 'ETH',
|
||||
LITECOIN = 'LTC',
|
||||
DOGECOIN = 'DOGE',
|
||||
MONERO = 'XMR'
|
||||
}
|
||||
|
||||
export enum FiatUnits {
|
||||
USD = 'USD',
|
||||
EUR = 'EURO'
|
||||
}
|
||||
|
||||
export enum PaymentStatus {
|
||||
/**
|
||||
* The payment has not been yet started. The user did not initiated the transfer.
|
||||
*/
|
||||
PENDING = 0,
|
||||
|
||||
/**
|
||||
* The payment has been made but it's not yet confirmed.
|
||||
*/
|
||||
UNCONFIRMED = 1,
|
||||
|
||||
/**
|
||||
* The payment is completed and the crypto is now available.
|
||||
*/
|
||||
DONE = 2
|
||||
}
|
||||
|
||||
export interface SellItem {
|
||||
price: {
|
||||
unit: number,
|
||||
currency:
|
||||
}
|
||||
}
|
||||
57
src/models/invoice.ts
Normal file
57
src/models/invoice.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { AfterLoad, Column, Entity, PrimaryColumn } from "typeorm";
|
||||
import { CryptoUnits, FiatUnits, PaymentStatus } from "../helper/types";
|
||||
|
||||
@Entity()
|
||||
export class Invoice {
|
||||
|
||||
@PrimaryColumn()
|
||||
id: number;
|
||||
|
||||
// Available payment methods
|
||||
// btc,xmr,eth,doge
|
||||
@Column({ type: 'text' })
|
||||
paymentMethods: CryptoUnits[];
|
||||
|
||||
// 1Kss3e9iPB9vTgWJJZ1SZNkkFKcFJXPz9t
|
||||
@Column()
|
||||
receiveAddress: string;
|
||||
|
||||
@Column()
|
||||
paidWith: CryptoUnits;
|
||||
|
||||
// Is set when invoice got paid
|
||||
// 3b38c3a215d4e7981e1516b2dcbf76fca58911274d5d55b3d615274d6e10f2c1
|
||||
@Column({ nullable: true })
|
||||
transcationHash: string;
|
||||
|
||||
@Column({ type: 'varchar' })
|
||||
priceUnit: FiatUnits;
|
||||
|
||||
@Column({ type: 'float' })
|
||||
price: number;
|
||||
|
||||
@Column()
|
||||
dueBy: number;
|
||||
|
||||
@Column({ type: 'smallint' })
|
||||
status: PaymentStatus;
|
||||
|
||||
@Column({ nullable: true })
|
||||
email: string;
|
||||
|
||||
@Column({ type: 'timestamp' })
|
||||
createdAt: number;
|
||||
|
||||
@AfterLoad()
|
||||
convertPayments() {
|
||||
if (this.paymentMethods !== undefined) {
|
||||
/*const arr = this.paymentMethods.split(',');
|
||||
let final: CryptoUnits[];
|
||||
|
||||
arr.forEach(elem => {
|
||||
final.push(CryptoUnits[elem.toUpperCase()]);
|
||||
});*/
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
8
src/routes/user.ts
Normal file
8
src/routes/user.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Router } from "express";
|
||||
|
||||
const invoiceRouter = Router()
|
||||
|
||||
invoiceRouter.get('/:id');
|
||||
invoiceRouter.post('/');
|
||||
|
||||
export { invoiceRouter };
|
||||
Reference in New Issue
Block a user