-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from AhmadReshadarm/master
Feauters/Notification
- Loading branch information
Showing
17 changed files
with
578 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { Basket } from './basket.entity'; | ||
export { OrderProduct } from "./orderProduct.entity"; | ||
export { OrderProduct } from './orderProduct.entity'; | ||
export { Address } from './address.entity'; | ||
export { Checkout } from './checkout.entity'; | ||
export { Subscription } from './subscrition.entity'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity() | ||
export class Subscription { | ||
@PrimaryGeneratedColumn() | ||
id: string; | ||
|
||
@Column('text', { nullable: true }) | ||
subscriber: string; | ||
|
||
constructor(args?: { subscriber: string }) { | ||
if (args) { | ||
this.subscriber = args.subscriber; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { CheckoutStatus } from 'core/enums/checkout-status.enum'; | ||
import webpush from 'web-push'; | ||
import { Role } from '../../core/enums/roles.enum'; | ||
import { Request, Response } from 'express'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { singleton } from 'tsyringe'; | ||
import { Controller, Delete, Get, Middleware, Post, Put } from '../../core/decorators'; | ||
import { Checkout } from '../../core/entities'; | ||
import { Checkout, Subscription } from '../../core/entities'; | ||
import { HttpStatus } from '../../core/lib/http-status'; | ||
import { validation } from '../../core/lib/validator'; | ||
import { isAdmin, isUser, verifyToken } from '../../core/middlewares'; | ||
import { createInvoice } from '../../orders/functions/createInvoice'; | ||
import { sendInvoice } from '../../orders/functions/send.mail'; | ||
import { CheckoutService } from './checkout.service'; | ||
import { invoiceTamplate } from '../functions/invoice.tamplate'; | ||
|
||
@singleton() | ||
@Controller('/checkouts') | ||
|
@@ -50,14 +51,71 @@ export class CheckoutController { | |
async createCheckout(req: Request, resp: Response) { | ||
const newCheckout = new Checkout(req.body); | ||
newCheckout.userId = resp.locals.user.id; | ||
const name = resp.locals.user.name; | ||
const { jwt } = resp.locals; | ||
let created: any; | ||
try { | ||
await validation(newCheckout); | ||
} catch (error) { | ||
console.log(`validation faild: ${error}`); | ||
} | ||
try { | ||
created = await this.checkoutService.createCheckout(newCheckout); | ||
resp.status(HttpStatus.CREATED).json(created); | ||
} catch (error) { | ||
resp.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: `saving order faild: ${error}` }); | ||
} | ||
try { | ||
const invoiceData: any = await createInvoice(created!, jwt.name); | ||
sendInvoice(invoiceTamplate(invoiceData), jwt.email); | ||
console.log('invoice send succsfuly'); | ||
} catch (error) { | ||
console.log(`sending invoice faild: ${error}`); | ||
} | ||
try { | ||
const subscrition = await this.checkoutService.getSubscribers(); | ||
if (!subscrition || subscrition.length === 0) return; | ||
const payload = JSON.stringify({ | ||
title: `Заказ №: ${created?.id}`, | ||
message: `Сума: ${created?.totalAmount}`, | ||
url: `https:wuluxe.ru/admin/checkouts/${created?.id}`, | ||
}); | ||
webpush.setVapidDetails( | ||
'mailto:[email protected]', | ||
process.env.NOTIFACATION_PUBLIC_KEY!, | ||
process.env.NOTIFACATION_PRIVATE_KEY!, | ||
); | ||
for (let i = 0; i < subscrition.length; i++) { | ||
webpush.sendNotification(JSON.parse(`${subscrition[i].subscriber}`), payload); | ||
} | ||
} catch (error) { | ||
console.log(`sending notification faild: ${error}`); | ||
} | ||
} | ||
|
||
await validation(newCheckout); | ||
const created = await this.checkoutService.createCheckout(newCheckout); | ||
@Post('subscribe') | ||
@Middleware([verifyToken, isAdmin]) | ||
async createSubscriber(req: Request, resp: Response, next: NextFunction) { | ||
try { | ||
const subscrition = await this.checkoutService.getSubscribers(); | ||
if (subscrition && subscrition.length !== 0) { | ||
for (let i = 0; i < subscrition.length; i++) { | ||
if (subscrition[i].subscriber === req.body.subscriber) { | ||
resp.status(HttpStatus.ACCEPTED).json({ message: 'Your are all set' }); | ||
return; | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
next(); | ||
} | ||
|
||
// const invoice = await createInvoice(created!, { name }); | ||
// sendInvoice(invoice, resp.locals.user.email); | ||
resp.status(HttpStatus.CREATED).json(created); | ||
try { | ||
const newSubscrition = await validation(new Subscription({ subscriber: req.body.subscriber })); | ||
const created = await this.checkoutService.createSubscriber(newSubscrition); | ||
resp.status(HttpStatus.OK).json(created); | ||
} catch (error) { | ||
resp.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ message: `somthing went wrong: ${error}` }); | ||
} | ||
} | ||
|
||
@Put(':id') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,35 @@ | ||
const niceInvoice = require('nice-invoice'); | ||
import axios from 'axios'; | ||
import { Checkout, OrderProduct } from 'core/entities'; | ||
const getProducts = async (orderProducts: OrderProduct[]) => { | ||
const products = []; | ||
for (const orderProduct of orderProducts) { | ||
const product = await axios.get(`${process.env.CATALOG_DB}/products/${orderProduct.productId}`); | ||
products.push({ | ||
item: product.data.name, | ||
name: product.data.name, | ||
description: `${product.data.desc.slice(0, 70)}...`, | ||
quantity: orderProduct.qty, | ||
price: orderProduct.productPrice, | ||
tax: '', | ||
}); | ||
} | ||
|
||
return products; | ||
}; | ||
const createInvoice = async (checkout: Checkout, user: { name: string }) => { | ||
const createInvoice = async (checkout: Checkout, userName: any) => { | ||
const invoiceDetail = { | ||
shipping: { | ||
name: user.name, | ||
name: userName, | ||
address: checkout.address.address, | ||
city: '_', | ||
state: '_', | ||
country: 'RU', | ||
door: checkout.address.door, | ||
floor: checkout.address.floor, | ||
receverName: checkout.address.receiverName, | ||
postal_code: checkout.address.zipCode, | ||
}, | ||
items: await getProducts(checkout.basket.orderProducts), | ||
|
||
subtotal: checkout.totalAmount, | ||
total: checkout.totalAmount, | ||
order_number: checkout.id, | ||
header: { | ||
company_name: 'Wuluxe', | ||
company_address: 'МО, г. Люберцы, Октябрьский проспект 181', | ||
}, | ||
footer: { | ||
text: 'wuluxe.ru', | ||
}, | ||
currency_symbol: '₽', | ||
date: { | ||
billing_date: new Date(checkout.createdAt).toLocaleDateString('ru-RU'), | ||
}, | ||
billingDate: checkout.createdAt, | ||
}; | ||
return invoiceDetail; | ||
// return niceInvoice(invoiceDetail, 'wuluxe.pdf'); | ||
}; | ||
|
||
export { createInvoice }; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.