Invoices can be created

- Transactions can now be tracked
This commit is contained in:
2020-12-25 13:28:19 +01:00
parent 0ffbe170dd
commit 16194dca8f
15 changed files with 968 additions and 531 deletions

View File

@@ -0,0 +1,42 @@
import { Document } from 'mongoose';
import { CryptoUnits, FiatUnits, PaymentStatus } from '../../helper/types';
export interface ICart {
price: number;
name: string;
image: string;
quantity: number;
}
export interface IInvoice extends Document {
// Available payment methods
// [btc, xmr, eth, doge]
paymentMethods: CryptoUnits[];
// 1Kss3e9iPB9vTgWJJZ1SZNkkFKcFJXPz9t
receiveAddress: string;
paidWith?: CryptoUnits;
// Is set when invoice got paid
// 3b38c3a215d4e7981e1516b2dcbf76fca58911274d5d55b3d615274d6e10f2c1
transcationHash?: string;
cart?: ICart[];
totalPrice?: number;
currency: FiatUnits;
// Time in minutes the user has to pay.
// Time left = (createdAt + dueBy) - Date.now() / 1000
dueBy: number;
status?: PaymentStatus;
// E-Mail address of user, if he want's a confirmation email.
email?: string;
successUrl: string;
cancelUrl: string;
createdAt?: number;
}

View File

@@ -0,0 +1,6 @@
import { Model, model } from 'mongoose';
import { IInvoice } from './invoice.interface';
import { schemaInvoice } from './invoice.schema';
const modelInvoice: Model<IInvoice> = model<IInvoice>('Invoice', schemaInvoice , 'Invoice');
export { modelInvoice as Invoice };

View File

@@ -0,0 +1,58 @@
import { NativeError, Schema, SchemaTypes } from 'mongoose';
import { CryptoUnits, FiatUnits, PaymentStatus } from '../../helper/types';
import { IInvoice } from './invoice.interface';
const urlRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/
const schemaCart = new Schema({
price: { type: Number, required: true },
name: { type: String, trim: true, required: true },
image: { type: String, match: urlRegex, required: true },
quantity: { type: Number, default: 1 }
})
const schemaInvoice = new Schema({
paymentMethods: [{ type: String, enum: Object.values(CryptoUnits), default: [CryptoUnits.BITCOIN], required: true }],
receiveAddress: { type: String, required: true },
paidWith: { type: String, enum: CryptoUnits },
transcationHash: { type: String, required: false },
cart: [{ type: schemaCart, required: false }],
totalPrice: { type: Number, required: false },
currency: { type: String, enum: Object.values(FiatUnits), required: false },
dueBy: { type: Number, required: true },
status: { type: Number, enum: Object.values(PaymentStatus), default: PaymentStatus.PENDING },
email: { type: String, required: false },
successUrl: { type: String, match: urlRegex, required: false },
cancelUrl: { type: String, match: urlRegex, required: false }
}, {
timestamps: {
createdAt: true,
},
versionKey: false
});
// Validate values
schemaInvoice.post('validate', function (res, next) {
let self = this as IInvoice;
// If cart is undefined and price too, error.
if ((self.cart === undefined || self.cart.length === 0) && self.totalPrice === undefined) {
next(new Error('Either cart or price has to be defined!'));
return;
}
// If cart is provided, calculate price.
if (self.cart !== undefined && self.totalPrice === undefined) {
let totalPrice = 0;
for (let i = 0; i < self.cart.length; i++) {
const item = self.cart[i];
totalPrice += item.price * item.quantity;
}
self.set({ totalPrice });
}
next();
})
export { schemaInvoice }

View File

@@ -0,0 +1,10 @@
import { Document } from 'mongoose';
export interface IUser extends Document {
name: string,
password: string,
salt: string,
lastLogin: Date,
twoFASecret?: string,
createdAt?: Date
}

View File

@@ -0,0 +1,6 @@
import { Model, model } from 'mongoose';
import { IUser } from "./user.interface";
import { schemaUser } from './user.schema';
const modelUser: Model<IUser> = model<IUser>('User', schemaUser , 'User');
export { modelUser as User };

View File

@@ -0,0 +1,15 @@
import { Schema } from 'mongoose';
const schemaUser = new Schema({
name: { type: String, required: true },
password: { type: String, required: true },
salt: { type: String, required: true },
twoFASecret: { type: String, required: false },
lastLogin: { type: Date, required: true, default: Date.now },
}, {
timestamps: {
createdAt: true
}
});
export { schemaUser }