Skip to content

Commit

Permalink
Merge pull request #5 from returns-modernization/RETURNS-544-update-c…
Browse files Browse the repository at this point in the history
…loud-function-to-persist-data-in-datastore

Returns 544 update cloud function to persist data in datastore
  • Loading branch information
Shweta Ghenand authored and GitHub Enterprise committed Nov 11, 2019
2 parents 25310b8 + ef46351 commit d3bdc0e
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 91 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM node:8-slim

ARG CREDENTIALS_PATH=firestore-credentials.json
WORKDIR /app
COPY files/src/package.json /app

Expand All @@ -10,7 +9,7 @@ COPY files/src/. /app
COPY files/src/.env /app/.env

# The credentialspath will never be versioncontrolled
COPY ${CREDENTIALS_PATH} /app/firestore-credentials.json
COPY files/src/firestore-credentials.json /app/firestore-credentials.json

EXPOSE 3000
CMD ["npm", "start"]
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ http POST http://localhost:{port_1}/persist

## Deploy on Cloud

1. Clone the repository
2. Create a `.env` file with the "PROJECT_ID and CREDENTIAL_PATH' keys that can be found in the `.env.example` file under the /files/src directory
3. Copy your Service Account Credentials files under the " /files/src directory" and rename it with "firestore-credentials.json"
4. Open command Prompt and navigate to the folder "raw-storage-abstraction/files/src"

gcloud functions deploy raw-storage-abstraction --runtime nodejs8 --trigger-http --entry-point app

4 changes: 2 additions & 2 deletions files/src/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PORT=3000
LOCAL_RUN=true
CREDENTIAL_PATH=../../../firestore-credentials.json
DATABASE_URL=https://sassy-go.firebaseio.com
CREDENTIAL_PATH=firestore-credentials.json
PROJECT_ID=ingka-isx-return-dev
21 changes: 7 additions & 14 deletions files/src/config/database-config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
const dotenv = require('dotenv');
const admin = require('firebase-admin');
var logger = require('../util/logger');
const { Datastore } = require('@google-cloud/datastore');
dotenv.config();

try {
var serviceAccount = require(process.env.CREDENTIAL_PATH);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.DATABASE_URL
})
} catch (err) {
logger.error("error occured while intializing app with firestore:", err);
}
const db = admin.firestore();

module.exports = db
module.exports = (() => {
return new Datastore({
projectId: process.env.PROJECT_ID,
keyFilename: process.env.CREDENTIAL_PATH
});
})();
9 changes: 5 additions & 4 deletions files/src/constants/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var constants = {};
constants.EVENT_NAME = "raw-sac";
constants.PERIST_EVENT_SUCESS_MSG = "A new resource have been created in the database";
constants.PATTERN="-";
const constants = {};
constants.ENTITY_KIND_NAME = "raw-sac";
constants.PERIST_EVENT_SUCCESS_MSG = "A new resource have been created in the database";
constants.PERIST_EVENT_FAILURE_MSG = "Error while inserting entirty in cloud datastore";
constants.PATTERN = "-";


module.exports = constants;
30 changes: 15 additions & 15 deletions files/src/dao/raw-sac-dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ const constants = require('../constants/constants');

module.exports = {

persistData: function (payload, documentId) {
return new Promise(function (resolve, reject) {
db.collection(constants.EVENT_NAME)
.doc(documentId)
.set(payload)
.then(function (result) {
logger.info("Document successfully written!");
return resolve(result);
})
.catch(function (error) {
console.log(error);
logger.error("Error while writing document: ", error);
return reject(error);
});
});
persistData: async (payload, keyIdentifier) => {
try {
const entity = {
key: db.key([constants.ENTITY_KIND_NAME, keyIdentifier]),
data: {
data: payload,
},
};
await db.upsert(entity);
logger.info("Raw sac entity inserted sucessfully")

} catch (error) {
logger.error("Error while inserting entirty in cloud datastore", error);
throw new Error(error);
}
}
}
4 changes: 1 addition & 3 deletions files/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
"url": "[email protected]:returns-modernization/raw-storage-abstraction.git"
},
"dependencies": {
"@firebase/testing": "^0.14.2",
"@google-cloud/datastore": "^4.5.2",
"@google-cloud/logging-winston": "^3.0.0",
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.2.0",
"firebase-admin": "^8.6.1",
"firebase-functions": "^3.3.0",
"http-status-codes": "^1.3.2",
"log4js": "^5.3.0",
"logger": "0.0.1",
Expand Down
25 changes: 13 additions & 12 deletions files/src/routes/raw-sac.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ const express = require('express');
const { check, validationResult } = require('express-validator');
const router = express.Router();
const service = require('../service/raw-sac-service');
const rules = [
check('payloadVersion').contains("0.4"),
check('payloadVersion').isLength({ min: 0 })
];


router.post('/persist', [
check('payloadVersion').contains("0.4"),
check('payloadVersion').isLength({ min: 0 })
], (req, res, ) => {

router.post('/persist', rules, async (req, res, ) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
logger.error(errors);
logger.error("Error occurs while validating the payload:", { errors: errors.array() });
return res.status(httpStatus.BAD_REQUEST)
.send(util.buildResponse({ errors: errors.array() }, null, httpStatus.BAD_REQUEST));
.send(util.buildResponse(errors, { errors: errors.array() }, httpStatus.BAD_REQUEST));
}
try {
const result = await service.persistData(req)
res.status(httpStatus.CREATED).send(result)
} catch (error) {
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(error);
}
service.persistData(req).then(function (result) {
return res.status(httpStatus.CREATED).send(result);
}, function (error) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).send(error);

});
});

module.exports = router;
21 changes: 11 additions & 10 deletions files/src/service/raw-sac-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ const constants = require('../constants/constants')

module.exports = {

persistData: function (req) {
return new Promise(function (resolve, reject) {
//generate ID for the document so If the document does not exist, it will be created. If the document does exist, its contents will be overwritten with the newly provided data.
var documentId = util.generateDocumentID(req.body);
eventDao.persistData(req.body, documentId).then(function (result) {
return resolve(util.buildResponse(null, constants.PERIST_EVENT_SUCESS_MSG, httpStatus.CREATED))
}, function (error) {
return reject(util.buildResponse(error, null, httpStatus.INTERNAL_SERVER_ERROR))
});
});
persistData: async (req) => {
try {
//generate key identifier for the entity which will overwrite an entity if it already exists in Datastore mode or insert (which requires that the entity key not already exist).
const identifierKey = util.generateKeyIdentifier(req.body);
await eventDao.persistData(req.body, identifierKey)
return Promise.resolve(util.buildResponse(null, constants.PERIST_EVENT_SUCCESS_MSG, httpStatus.CREATED));
} catch (error) {
return Promise.reject(util.buildResponse(error, constants.PERIST_EVENT_FAILURE_MSG, httpStatus.INTERNAL_SERVER_ERROR));

}

}
}
11 changes: 4 additions & 7 deletions files/src/test/api-endpoint-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
var common = require('./common-setup');
var server = require('../index');
let payload = require("../test/arrange/event-payload.json");
let should = common.chai.should();
const firebase = require("@firebase/testing");
const projectId = "firestore-emulator-example";
firebase.initializeTestApp({ projectId, uid: "test" }).firestore();
const common = require('./common-setup');
const server = require('../index');
const payload = require("../test/arrange/event-payload.json");
const should = common.chai.should();
common.chai.use(common.chaiHttp);

describe(" tests", function () {
Expand Down
6 changes: 3 additions & 3 deletions files/src/test/api-validation-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var common = require('./common-setup');
var server = require('../index');
let should = common.chai.should();
const common = require('./common-setup');
const server = require('../index');
const should = common.chai.should();
common.chai.use(common.chaiHttp);

describe(" tests", function () {
Expand Down
12 changes: 6 additions & 6 deletions files/src/test/unit-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var util = require('../util/common-util');
let payload = require("../test/arrange/event-payload.json");
var expect = require('./common-setup').expect;
const util = require('../util/common-util');
const payload = require("../test/arrange/event-payload.json");
const expect = require('./common-setup').expect;

describe(" tests", function () {
it('generate document ID test', function (done) {
//ARRANGE
let expectedId = "S1NDLTEwMDMxODkzLVNF";
let expectedKeyIdentifier = "S1NDLTEwMDMxODkzLVNF";

//ACT
let actualID = util.generateDocumentID(payload)
let actualID = util.generateKeyIdentifier(payload)

//ASSERT
expect(actualID).to.equal(expectedId);
expect(actualID).to.equal(expectedKeyIdentifier);
done();
});

Expand Down
20 changes: 7 additions & 13 deletions files/src/util/common-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ const constants = require('../constants/constants')

module.exports = {

buildResponse: function (error, data, code) {
var response = {};
if (error) {
response.error = true;
response.message = error;
response.statusCode = code;

} else {
response.error = false;
response.message = data;
response.statusCode = code;
}
buildResponse: (error, message, code) => {
const response = {
error: error ? true : false,
message: message,
code: code
};
return response;
},

generateDocumentID: function (payload) {
generateKeyIdentifier: (payload) => {
let encodeValue = Buffer.from(payload.sacHead.sacUnit.concat(constants.PATTERN, payload.sacHead.sacNo, constants.PATTERN, payload.sacHead.countryCode));
return encodeValue.toString('base64')
}
Expand Down

0 comments on commit d3bdc0e

Please sign in to comment.