diff --git a/.github/workflows/server-api-deploy.yml b/.github/workflows/server-api-deploy.yml index f5ea83e..f41ef0e 100644 --- a/.github/workflows/server-api-deploy.yml +++ b/.github/workflows/server-api-deploy.yml @@ -25,7 +25,7 @@ jobs: run: | echo "##[set-output name=list;]$(echo $(git remote -v) | cut -d' ' -f2) ${{ steps.BaseRef.outputs.branch }} $GITHUB_REPOSITORY" id: Arguments - - name: Decrypt Environment and Send Data + - name: Decrypt Environment and build env: LARGE_SECRET_PASSPHRASE: ${{ secrets.PRIVATEKEY }} host: ${{ secrets[steps.HostName.outputs.host] }} @@ -44,15 +44,9 @@ jobs: else touch ../../.env fi - - name: copy file via ssh password - uses: appleboy/scp-action@master - with: - host: ${{ secrets[steps.HostName.outputs.host] }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - port: ${{ secrets.PORT }} - source: ".env" - target: "~/" + yarn build + cd ../../ + sshpass -p$passowrd scp -P $port -o StrictHostKeyChecking=no . $username@$password:~/ - name: deploy to server uses: appleboy/ssh-action@master env: diff --git a/apis/product/db/model/product.js b/apis/product/db/model/product.js index b9a6294..97c2f48 100644 --- a/apis/product/db/model/product.js +++ b/apis/product/db/model/product.js @@ -123,6 +123,7 @@ const productSchema = new Schema({ timestamps: { createdAt: true, updatedAt: true }, }); + productSchema.plugin(mongoosastic, { hosts: [ `${process.env.ELASTICSEARCH}`, diff --git a/apis/product/db/seeds/product.js b/apis/product/db/seeds/product.js index ac913f8..0db3241 100644 --- a/apis/product/db/seeds/product.js +++ b/apis/product/db/seeds/product.js @@ -4,8 +4,6 @@ import mock from './20191209.json'; const { product } = model; -require('dotenv').config(); - mongoose.connect(`${process.env.MONGO_URL}`, { useNewUrlParser: true, useUnifiedTopology: true, diff --git a/apis/product/env.ssl b/apis/product/env.ssl index 66ab652..a6bbe03 100644 Binary files a/apis/product/env.ssl and b/apis/product/env.ssl differ diff --git a/apis/product/package.json b/apis/product/package.json index 1d00e22..f130044 100644 --- a/apis/product/package.json +++ b/apis/product/package.json @@ -9,7 +9,7 @@ ] }, "scripts": { - "test": "jest --runInBand", + "test": "jest --runInBand --setupFiles dotenv/config --forceExit", "precommit": "lint-staged && yarn test", "dev:start": "babel-node bin/www.js", "build": "babel ./ -d build --copy-files '*.json'", diff --git a/apis/product/routes/controller/deleteProduct.js b/apis/product/routes/controller/deleteProduct.js deleted file mode 100644 index 9fb5d39..0000000 --- a/apis/product/routes/controller/deleteProduct.js +++ /dev/null @@ -1,12 +0,0 @@ -import { removeProduct } from '../../core'; - -const deleteProductController = async ({ params: { id } }, res, next) => { - try { - const result = await removeProduct(id, res.locals.userId); - res.json(result); - } catch (e) { - next(e); - } -}; - -export default deleteProductController; diff --git a/apis/product/routes/controller/getProductInfo.js b/apis/product/routes/controller/getProductInfo.js deleted file mode 100644 index 0cfbc71..0000000 --- a/apis/product/routes/controller/getProductInfo.js +++ /dev/null @@ -1,29 +0,0 @@ -import { getProducts, getElasticSearchResults } from '../../core'; - -const getProductListController = async (req, res, next) => { - if (res.locals.filter) { - res.locals.query = { - bool: { - must: res.locals.filter, - }, - }; - delete res.locals.filter; - } - try { - const list = await getElasticSearchResults(res.locals); - res.json(list); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -const findProductByIdController = async ({ params: { id } }, res, next) => { - try { - const result = await getProducts(1, 1, { _id: id }); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export { getProductListController, findProductByIdController }; diff --git a/apis/product/routes/controller/info.js b/apis/product/routes/controller/info.js new file mode 100644 index 0000000..e008db6 --- /dev/null +++ b/apis/product/routes/controller/info.js @@ -0,0 +1,8 @@ +import { getProductSchemaByKey } from '../../core'; + +const getProductSchemaController = (req, res) => { + const list = getProductSchemaByKey('category').enumValues; + res.json(list); +}; + +export default getProductSchemaController; diff --git a/apis/product/routes/controller/modifyProduct.js b/apis/product/routes/controller/modifyProduct.js deleted file mode 100644 index ed32a81..0000000 --- a/apis/product/routes/controller/modifyProduct.js +++ /dev/null @@ -1,14 +0,0 @@ -import { updateProduct } from '../../core'; - -const modifyProductController = async (req, res, next) => { - const { body, params: { id } } = req; - const { locals: { userId } } = res; - try { - const result = await updateProduct(id, userId, body); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export default modifyProductController; diff --git a/apis/product/routes/controller/productListLookup.js b/apis/product/routes/controller/productListLookup.js deleted file mode 100644 index 2d9840e..0000000 --- a/apis/product/routes/controller/productListLookup.js +++ /dev/null @@ -1,18 +0,0 @@ -import { getProducts } from '../../core'; - -const productListLookupController = async (req, res, next) => { - const { - options = {}, - sort = { order: -1, createAt: 1 }, - page = 1, - limits = 10, - } = res.lcoals; - try { - const result = await getProducts(options, sort, page, limits); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export default productListLookupController; diff --git a/apis/product/routes/controller/products.js b/apis/product/routes/controller/products.js new file mode 100644 index 0000000..edbf4f3 --- /dev/null +++ b/apis/product/routes/controller/products.js @@ -0,0 +1,77 @@ +import { + removeProduct, + getProducts, + getElasticSearchResults, + updateProduct, + insertProduct, +} from '../../core'; + +export const deleteProductController = async ({ params: { id } }, res, next) => { + try { + const result = await removeProduct(id, res.locals.userId); + res.json(result); + } catch (e) { + next(e); + } +}; + +export const getProductListController = async (req, res, next) => { + if (res.locals.filter) { + res.locals.query = { + bool: { + must: res.locals.filter, + }, + }; + delete res.locals.filter; + } + try { + const list = await getElasticSearchResults(res.locals); + res.json(list); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const findProductByIdController = async ({ params: { id } }, res, next) => { + try { + const result = await getProducts(1, 1, { _id: id }); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const modifyProductController = async (req, res, next) => { + const { body, params: { id } } = req; + const { locals: { userId } } = res; + try { + const result = await updateProduct(id, userId, body); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const productListLookupController = async (req, res, next) => { + const { + options = {}, + sort = { order: -1, createAt: 1 }, + page = 1, + limits = 10, + } = res.lcoals; + try { + const result = await getProducts(options, sort, page, limits); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const writeProductCotroller = async ({ body }, res, next) => { + try { + const result = await insertProduct(body); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; diff --git a/apis/product/routes/controller/writeProduct.js b/apis/product/routes/controller/writeProduct.js deleted file mode 100644 index ed8c7a7..0000000 --- a/apis/product/routes/controller/writeProduct.js +++ /dev/null @@ -1,12 +0,0 @@ -import { insertProduct } from '../../core'; - -const writeProductCotroller = async ({ body }, res, next) => { - try { - const result = await insertProduct(body); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export default writeProductCotroller; diff --git a/apis/product/routes/info.js b/apis/product/routes/info.js index fba9558..e93460c 100644 --- a/apis/product/routes/info.js +++ b/apis/product/routes/info.js @@ -1,11 +1,8 @@ import express from 'express'; -import { getProductSchemaByKey } from '../core'; +import getProductSchemaController from './controller/info'; const router = express.Router(); -router.get('/category', (req, res) => { - const list = getProductSchemaByKey('category').enumValues; - res.json(list); -}); +router.get('/category', getProductSchemaController); export default router; diff --git a/apis/product/routes/product.js b/apis/product/routes/product.js index b5b19dc..46fee81 100644 --- a/apis/product/routes/product.js +++ b/apis/product/routes/product.js @@ -1,10 +1,13 @@ import express from 'express'; -import modifyProductController from './controller/modifyProduct'; -import deleteProductController from './controller/deleteProduct'; -import writeProductCotroller from './controller/writeProduct'; import isLoggedInUser from '../services/user'; -import { getProductListController, findProductByIdController } from './controller/getProductInfo'; import queryAnalysisMiddleware from './middleware/listView'; +import { + modifyProductController, + deleteProductController, + writeProductCotroller, + getProductListController, + findProductByIdController, +} from './controller/products'; const router = express.Router(); diff --git a/apis/product/run.sh b/apis/product/run.sh index 0c4f2c9..d8b5827 100644 --- a/apis/product/run.sh +++ b/apis/product/run.sh @@ -28,8 +28,5 @@ fi PM2INSTALL=$(npm ls -g --depth=0 | grep -w -o pm2) if [ "pm2" != "$PM2INSTALL" ]; then npm install -g pm2 + pm2 start ./build/bin/www --name server fi - -yarn build -cd ./build -pm2 start ./bin/www --name server diff --git a/apis/product/tests/unit.test.js b/apis/product/tests/unit.test.js index ffec43a..369811a 100644 --- a/apis/product/tests/unit.test.js +++ b/apis/product/tests/unit.test.js @@ -1,16 +1,14 @@ /* eslint-disable no-underscore-dangle */ // https://www.mockaroo.com/ import mongoose from 'mongoose'; -import dotenv from 'dotenv'; import model from '../db/model'; -import mockData from '../db/seeds/20191129.json'; +import mockData from '../db/seeds/20191209.json'; import message from '../core/message'; import * as Core from '../core'; import CODE from '../core/code'; const Product = model.product; -dotenv.config(); jest.setTimeout(10000); beforeAll(async () => {