Skip to content
This repository has been archived by the owner on Feb 20, 2020. It is now read-only.

Add fetch data, export xlsx and send mail #17

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 98 additions & 36 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,100 @@
version: 2 # use CircleCI 2.0
jobs: # a collection of steps
build: # runs not using Workflows must have a `build` job as entry point
working_directory: ~/mern-starter # directory where steps will run
docker: # run the steps with Docker
- image: circleci/node:4.8.2 # ...with this image as the primary container; this is where all `steps` will run
- image: mongo:3.4.4 # and this image as the secondary service container
steps: # a collection of executable commands
- checkout # special step to check out source code to working directory
version: 2
jobs:
build:
docker:
- image: circleci/node:lts
working_directory: ~/app/reporter
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run:
name: update-npm
command: 'sudo npm install -g npm@latest'
- restore_cache: # special step to restore the dependency cache
# Read about caching dependencies: https://circleci.com/docs/2.0/caching/
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: install-npm-wee
command: npm install
- save_cache: # special step to save the dependency cache
key: dependency-cache-{{ checksum "package.json" }}
name: Install dependencies
command: yarn install && ls -l node_modules
- run: pwd && ls -la
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- persist_to_workspace:
root: ~/app
paths:
- ./node_modules
- run: # run tests
name: test
command: npm test
- run: # run coverage report
name: code-coverage
command: './node_modules/.bin/nyc report --reporter=text-lcov'
- store_artifacts: # special step to save test results as as artifact
# Upload test summary for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: test-results.xml
prefix: tests
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: coverage
prefix: coverage
- store_test_results: # for display in Test Summary: https://circleci.com/docs/2.0/collect-test-data/
path: test-results.xml
# See https://circleci.com/docs/2.0/deployment-integrations/ for deploy examples
- reporter
report-order:
docker:
- image: circleci/node:lts
working_directory: ~/app/reporter
steps:
- attach_workspace:
at: ~/app
- run: pwd && ls -la
- run:
name: Fetch ORDER data, create xlsx and send mail
command: yarn order:ci

report-product:
docker:
- image: circleci/node:lts
working_directory: ~/app/reporter
steps:
- attach_workspace:
at: ~/app
- run: pwd && ls -la
- run:
name: Fetch PRODUCT data, create xlsx and send mail
command: yarn product:ci

report-category:
docker:
- image: circleci/node:lts
working_directory: ~/app/reporter
steps:
- attach_workspace:
at: ~/app
- run: pwd && ls -la
- run:
name: Fetch CATEGORY data, create xlsx and send mail
command: yarn category:ci

workflows:
version: 2
Orders Report:
triggers:
- schedule:
cron: '0 2 * * *'
filters:
branches:
only: master
jobs:
- build
- report-order:
requires:
- build

Product Report:
triggers:
- schedule:
cron: '0 2 * * 1'
filters:
branches:
only: master
jobs:
- build
- report-product:
requires:
- build

Category Report:
triggers:
- schedule:
cron: '0 2 * * 1'
filters:
branches:
only: master
jobs:
- build
- report-category:
requires:
- build
16 changes: 15 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
TOKEN_KEY=
# root path export data, default 'exports'
EXPORTS_PATH=
# email of user that get token key
MAIL_USER=
MAIL_CLIENT_ID=
MAIL_CLIENT_SECRET=
MAIL_REFRESH_TOKEN=
MAIL_ACCESS_TOKEN=
MAIL_EXPIRES=
# time period from today to fetch data, ex '15'
DURATION_DAYS_GET_ORDERS=
# email recieve: ex: [email protected], [email protected]
MAIL_SEND_TO=
# email address that send this mail
MAIL_SEND_FROM=
# email recieve as cc mail
MAIL_SEND_CC=
# email recieve as bcc mail
MAIL_SEND_BCC=
MAIL_SUBJECT=
# title of email,common use in ProductReport, OrderReport, VariantReport. ex: 'Report Data'
MAIL_TITLE=
# number of resend times if an error occurs
MAX_TIME_RETRY_SEND_MAIL=
# token to send notification ex: {{https://hooks.slack.com/services/}}SLACK_TOKEN
SLACK_TOKEN=


3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules/
dist/
.env
docker-compose.yml
exports/
orders.json

Binary file removed exportData
Binary file not shown.
22 changes: 16 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "jest",
"test": "set NODE_ENV=development && jest --setupFiles dotenv/config",
"tsc": "tsc",
"dev": "tsnd -r dotenv/config --respawn src/services/order.ts",
"product": "tsnd -r dotenv/config --respawn src/services/product.ts",
"category": "tsnd -r dotenv/config --respawn src/services/category.ts",
"dev:production-order": "set NODE_ENV=production && tsnd -r dotenv/config --respawn src/services/reports/order.ts"
"dev": "tsnd -r dotenv/config --respawn --transpileOnly src/services/reports/order.ts",
"product": "set NODE_ENV=production && tsnd -r dotenv/config --respawn --transpileOnly src/services/reports/product.ts",
"category": "set NODE_ENV=production && tsnd -r dotenv/config --respawn --transpileOnly src/services/reports/category.ts",
"order": "set NODE_ENV=production && tsnd -r dotenv/config --respawn --transpileOnly src/services/reports/order.ts",
"order:ci": "set NODE_ENV=production && ts-node -r dotenv/config --transpile-only src/services/reports/order.ts",
"product:ci": "set NODE_ENV=production && ts-node -r dotenv/config --transpile-only src/services/reports/product.ts",
"category:ci": "set NODE_ENV=production && ts-node -r dotenv/config --transpile-only src/services/reports/category.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/moment-timezone": "^0.5.12",
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"graphql": "^14.2.1",
"lodash": "^4.17.11",
"moment": "^2.24.0",
"moment-timezone": "^0.5.23",
"node-xlsx": "^0.14.1",
"nodemailer": "^6.1.0",
"object-assign": "^4.1.1"
Expand All @@ -29,7 +34,6 @@
"@types/nodemailer": "4.6.7",
"@types/dotenv": "6.1.1",
"@types/jest": "24.0.11",
"@types/lodash": "4.14.123",
"dotenv": "7.0.0",
"jest": "24.7.1",
"nodemon": "1.18.11",
Expand Down
33 changes: 17 additions & 16 deletions src/services/__test__/order.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from 'moment'
import {getData} from "../order";
import { fetchOrders } from 'services/order';
// import {getData} from "../order";

var since = moment().startOf('day').subtract(1, 'days').format()
// since.toStrin
Expand All @@ -10,21 +11,21 @@ var since = moment().startOf('day').subtract(1, 'days').format()
// });
// });

describe("Test order services", () => {
it("Test since and util is not empty", async () => {
// expect.assertions(1)
try {
await getData({
since: new Date(),
util: '',
})
// describe("Test order services", () => {
// it("Test since and util is not empty", async () => {
// // expect.assertions(1)
// try {
// await fetchOrders({
// since: new Date(),
// util: '',
// })

} catch (err) {
console.log(err)
expect(err).toMatchObject({
// } catch (err) {
// console.log(err)
// expect(err).toMatchObject({

})
}
});
});
// })
// }
// });
// });

20 changes: 20 additions & 0 deletions src/services/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from 'axios'
const request = axios.create({
baseURL: 'https://api.storelammoc.vn/',
timeout: 15e3,
headers: { 'x-access-token': process.env.TOKEN_KEY },
})

export const fetchCategories = async (options: { limit?: number; variantSKU?: string }) => {
try {
const response = await request.get(`/v2/categories`, {
params: {
...options,
},
})

return response.data
} catch (error) {
throw error
}
}
59 changes: 59 additions & 0 deletions src/services/mail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import nodemailer, { SentMessageInfo } from 'nodemailer'

const MAX_TIME_RETRY_SEND_MAIL = process.env.MAX_TIME_RETRY_SEND_MAIL || 3
export const sendMail = async (
options: {
mailFrom: string
mailTo: string
mailCc?: any
mailBcc?: any
title: string
fileName?: string
filePath?: string
},
retryCount = 0,
): Promise<SentMessageInfo> => {
try {
console.log('Start send mail to', options.mailTo)
const transporterOptions: any = {
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: process.env.MAIL_USER,
clientId: process.env.MAIL_CLIENT_ID,
clientSecret: process.env.MAIL_CLIENT_SECRET,
refreshToken: process.env.MAIL_REFRESH_TOKEN,
accessToken: process.env.MAIL_ACCESS_TOKEN,
expires: process.env.MAIL_EXPIRES,
// accessUrl:
},
}
const transporter = nodemailer.createTransport(transporterOptions)

await transporter.verify()

return await transporter.sendMail({
from: options.mailFrom,
to: options.mailTo,
bcc: options.mailBcc,
cc: options.mailCc,
subject: options.title,
attachments: [
{
filename: options.fileName,
path: options.filePath,
},
],
})
} catch (err) {
console.log(retryCount)
if (retryCount < MAX_TIME_RETRY_SEND_MAIL) {
return await sendMail(options, retryCount + 1)
}
throw err
} finally {
console.log('Send Mail Done !')
}
}
Loading