{"action":"created","comment":{"url":"https://api.github.com/repos/danliris/bateeq-module/pulls/comments/129499694","pull_request_review_id":52277971,"id":129499694,"diff_hunk":"@@ -0,0 +1,185 @@\n+'use strict';\n+\n+// external deps \n+var ObjectId = require('mongodb').ObjectId;\n+\n+// internal deps\n+require('mongodb-toolkit');\n+var lodash_ = require('lodash'); // _ is already used by underscore.js\n+var BateeqModels = require('bateeq-models');\n+var map = BateeqModels.map;\n+var BaseManager = require('module-toolkit').BaseManager;\n+var Storage = BateeqModels.master.storage\n+var dateFormat = require('dateformat');\n+\n+module.exports = class StockAvailabilityManager extends BaseManager {\n+\n+    constructor(db, user) {\n+        super(db, user);\n+        this.collection = this.db.use(map.inventory.Inventory);\n+        this.storageCollection = this.db.use(map.master.Storage);\n+        this.storeCollection = this.db.use(map.master.Store);\n+        this.invMovementCollection = this.db.use(map.inventory.InventoryMovement);\n+    }\n+\n+    // _validate(storageId) {\n+    //     var errors = {};\n+    //     return new Promise((resolve, reject) => {\n+    //         var id;\n+    //         storageId != \"\" ? id = new ObjectId(storageId) : id = storageId;\n+    //         if (storageId == \"\") {\n+    //             errors[\"codeStorage\"] = \"Kode storage yang anda cari tidak dapat ditemukan\";\n+    //             resolve(errors.codeStorage);\n+    //         }\n+    //         else {\n+    //             var getStorage = this.storageCollection.find({ _id: id })\n+    //                 .toArray((err, result) => {\n+    //                     var _storage = result[0];\n+    //                     resolve(_storage._id);\n+    //                 });\n+    //         }\n+    //     });\n+    // }\n+\n+    getStorageStock(storageId) {\n+        // return new Promise((resolve, reject) => {\n+        //     this._validate(storageId)\n+        //         .then(validStorageId => {\n+        //             if (typeof validStorageId == \"string\") {\n+        //                 resolve(result);\n+        //             }\n+        //             else {\n+        //                 let id = validStorageId\n+        //                 var find = { \"storage._id\": id };\n+        //                 var sort = { quantity: 1 };\n+        //                 this.collection.find(find).sort(sort)\n+        //                     .toArray((err, result) => {\n+        //                         resolve(result);\n+        //                     });\n+        //             }\n+        //         })  \n+        // });\n+        return new Promise((resolve, reject) => {\n+            let id = new ObjectId(storageId);\n+            var find = { \"storage._id\": id };\n+            var sort = { quantity: 1 };\n+            this.collection.find(find).sort(sort)\n+                .toArray((err, result) => {\n+                    resolve(result);\n+                })\n+        });\n+    }\n+\n+\n+    getNearestStock(inventoryId) {\n+        let id = new ObjectId(inventoryId);\n+        let inventory = new Promise((resolve, reject) => {\n+            this.collection.find({ _id: id })\n+                .toArray((err, result) => {\n+                    resolve(result);\n+                });\n+        });\n+        let inventoryDb = new Promise((resolve, reject) => {\n+            inventory.then(result => {\n+                this.collection.find({ \"item.code\": result[0].item.code })\n+                    .toArray((err, result) => {\n+                        resolve(result);\n+                    })\n+            })\n+        });\n+        let storeDb = new Promise((resolve, reject) => {\n+            this.storeCollection.find()\n+                .toArray((err, result) => {\n+                    resolve(result);\n+                });\n+        });\n+        let invMovementDb = new Promise((resolve, reject) => {\n+            this.invMovementCollection.find()\n+                .toArray((err, result) => {\n+                    resolve(result);\n+                });\n+        });\n+        return new Promise((resolve, reject) => {\n+            Promise.all([inventoryDb, storeDb, invMovementDb])\n+                .then(array => {\n+                    let inventoryArray = array[0];\n+                    let storeArray = array[1];\n+                    let invMovementArray = array[2];\n+                    let data = [];\n+                    let uniqueStorage = [];\n+                    for (let inventory of inventoryArray) {\n+                        let dates = [];\n+                        for (let store of storeArray) {\n+                            if (inventory.storage.code === store.storage.code) {\n+                                inventory.city = store.city;\n+                            }\n+                        }\n+                        for (let invMovement of invMovementArray) {\n+                            if (inventory.storage.code === invMovement.storage.code &&\n+                                invMovement.type === \"IN\") {\n+                                dates.push(invMovement._createdDate);\n+                            }\n+                        }\n+                        let latestDate = new Date(Math.max.apply(null, dates));\n+                        let latestDateFormatted = dateFormat(latestDate, \"mm/dd/yyyy\");\n+                        inventory.latestDate = latestDateFormatted;\n+                        // if (!uniqueStorage.includes(inventory.storage.code)){\n+                        //     uniqueStorage.push(inventory.storage.code);\n+                        //     data.push(inventory);\n+                        // } else {\n+                        //     let uniqueInventory = data.find((inv) => {\n+                        //         return inv.storage.code == inventory.storage.code;\n+                        //     })\n+                        //     uniqueInventory.quantity += inventory.quantity;\n+                        // }\n+                        data.push(inventory);\n+                    }\n+                    let thisInventory = lodash_.find(data, { _id: id });\n+                    let sameCityInventory = lodash_.filter(data, { city: thisInventory.city });\n+                    lodash_.remove(data, { city: thisInventory.city });\n+                    sameCityInventory = lodash_.orderBy(sameCityInventory, 'quantity', 'desc');\n+                    data = lodash_.orderBy(data, 'quantity', 'desc');\n+                    for (let inventory of data) {","path":"src/managers/inventory/stock-availability-manager.js","position":142,"original_position":142,"commit_id":"50634079a9f8ea9776bfd65cbfdea4c9ab4aabc8","original_commit_id":"50634079a9f8ea9776bfd65cbfdea4c9ab4aabc8","user":{"login":"houndci-bot","id":6697940,"avatar_url":"https://avatars0.githubusercontent.com/u/6697940?v=4","gravatar_id":"","url":"https://api.github.com/users/houndci-bot","html_url":"https://github.com/houndci-bot","followers_url":"https://api.github.com/users/houndci-bot/followers","following_url":"https://api.github.com/users/houndci-bot/following{/other_user}","gists_url":"https://api.github.com/users/houndci-bot/gists{/gist_id}","starred_url":"https://api.github.com/users/houndci-bot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/houndci-bot/subscriptions","organizations_url":"https://api.github.com/users/houndci-bot/orgs","repos_url":"https://api.github.com/users/houndci-bot/repos","events_url":"https://api.github.com/users/houndci-bot/events{/privacy}","received_events_url":"https://api.github.com/users/houndci-bot/received_events","type":"User","site_admin":false},"body":"'for of' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).<br>'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).","created_at":"2017-07-26T07:36:14Z","updated_at":"2017-07-26T07:36:14Z","html_url":"https://github.com/danliris/bateeq-module/pull/341#discussion_r129499694","pull_request_url":"https://api.github.com/repos/danliris/bateeq-module/pulls/341","_links":{"self":{"href":"https://api.github.com/repos/danliris/bateeq-module/pulls/comments/129499694"},"html":{"href":"https://github.com/danliris/bateeq-module/pull/341#discussion_r129499694"},"pull_request":{"href":"https://api.github.com/repos/danliris/bateeq-module/pulls/341"}}},"pull_request":{"url":"https://api.github.com/repos/danliris/bateeq-module/pulls/341","id":132448881,"html_url":"https://github.com/danliris/bateeq-module/pull/341","diff_url":"https://github.com/danliris/bateeq-module/pull/341.diff","patch_url":"https://github.com/danliris/bateeq-module/pull/341.patch","issue_url":"https://api.github.com/repos/danliris/bateeq-module/issues/341","number":341,"state":"open","locked":false,"title":"Dev","user":{"login":"fendywillyam","id":24405641,"avatar_url":"https://avatars3.githubusercontent.com/u/24405641?v=4","gravatar_id":"","url":"https://api.github.com/users/fendywillyam","html_url":"https://github.com/fendywillyam","followers_url":"https://api.github.com/users/fendywillyam/followers","following_url":"https://api.github.com/users/fendywillyam/following{/other_user}","gists_url":"https://api.github.com/users/fendywillyam/gists{/gist_id}","starred_url":"https://api.github.com/users/fendywillyam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fendywillyam/subscriptions","organizations_url":"https://api.github.com/users/fendywillyam/orgs","repos_url":"https://api.github.com/users/fendywillyam/repos","events_url":"https://api.github.com/users/fendywillyam/events{/privacy}","received_events_url":"https://api.github.com/users/fendywillyam/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-07-26T07:36:06Z","updated_at":"2017-07-26T07:36:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"acc19f3beae059158fcf870ca7ca43ae6bde1f18","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/danliris/bateeq-module/pulls/341/commits","review_comments_url":"https://api.github.com/repos/danliris/bateeq-module/pulls/341/comments","review_comment_url":"https://api.github.com/repos/danliris/bateeq-module/pulls/comments{/number}","comments_url":"https://api.github.com/repos/danliris/bateeq-module/issues/341/comments","statuses_url":"https://api.github.com/repos/danliris/bateeq-module/statuses/50634079a9f8ea9776bfd65cbfdea4c9ab4aabc8","head":{"label":"danliris:dev","ref":"dev","sha":"50634079a9f8ea9776bfd65cbfdea4c9ab4aabc8","user":{"login":"danliris","id":20855729,"avatar_url":"https://avatars0.githubusercontent.com/u/20855729?v=4","gravatar_id":"","url":"https://api.github.com/users/danliris","html_url":"https://github.com/danliris","followers_url":"https://api.github.com/users/danliris/followers","following_url":"https://api.github.com/users/danliris/following{/other_user}","gists_url":"https://api.github.com/users/danliris/gists{/gist_id}","starred_url":"https://api.github.com/users/danliris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danliris/subscriptions","organizations_url":"https://api.github.com/users/danliris/orgs","repos_url":"https://api.github.com/users/danliris/repos","events_url":"https://api.github.com/users/danliris/events{/privacy}","received_events_url":"https://api.github.com/users/danliris/received_events","type":"User","site_admin":false},"repo":{"id":61858352,"name":"bateeq-module","full_name":"danliris/bateeq-module","owner":{"login":"danliris","id":20855729,"avatar_url":"https://avatars0.githubusercontent.com/u/20855729?v=4","gravatar_id":"","url":"https://api.github.com/users/danliris","html_url":"https://github.com/danliris","followers_url":"https://api.github.com/users/danliris/followers","following_url":"https://api.github.com/users/danliris/following{/other_user}","gists_url":"https://api.github.com/users/danliris/gists{/gist_id}","starred_url":"https://api.github.com/users/danliris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danliris/subscriptions","organizations_url":"https://api.github.com/users/danliris/orgs","repos_url":"https://api.github.com/users/danliris/repos","events_url":"https://api.github.com/users/danliris/events{/privacy}","received_events_url":"https://api.github.com/users/danliris/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/danliris/bateeq-module","description":"bateeq module","fork":false,"url":"https://api.github.com/repos/danliris/bateeq-module","forks_url":"https://api.github.com/repos/danliris/bateeq-module/forks","keys_url":"https://api.github.com/repos/danliris/bateeq-module/keys{/key_id}","collaborators_url":"https://api.github.com/repos/danliris/bateeq-module/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/danliris/bateeq-module/teams","hooks_url":"https://api.github.com/repos/danliris/bateeq-module/hooks","issue_events_url":"https://api.github.com/repos/danliris/bateeq-module/issues/events{/number}","events_url":"https://api.github.com/repos/danliris/bateeq-module/events","assignees_url":"https://api.github.com/repos/danliris/bateeq-module/assignees{/user}","branches_url":"https://api.github.com/repos/danliris/bateeq-module/branches{/branch}","tags_url":"https://api.github.com/repos/danliris/bateeq-module/tags","blobs_url":"https://api.github.com/repos/danliris/bateeq-module/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/danliris/bateeq-module/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/danliris/bateeq-module/git/refs{/sha}","trees_url":"https://api.github.com/repos/danliris/bateeq-module/git/trees{/sha}","statuses_url":"https://api.github.com/repos/danliris/bateeq-module/statuses/{sha}","languages_url":"https://api.github.com/repos/danliris/bateeq-module/languages","stargazers_url":"https://api.github.com/repos/danliris/bateeq-module/stargazers","contributors_url":"https://api.github.com/repos/danliris/bateeq-module/contributors","subscribers_url":"https://api.github.com/repos/danliris/bateeq-module/subscribers","subscription_url":"https://api.github.com/repos/danliris/bateeq-module/subscription","commits_url":"https://api.github.com/repos/danliris/bateeq-module/commits{/sha}","git_commits_url":"https://api.github.com/repos/danliris/bateeq-module/git/commits{/sha}","comments_url":"https://api.github.com/repos/danliris/bateeq-module/comments{/number}","issue_comment_url":"https://api.github.com/repos/danliris/bateeq-module/issues/comments{/number}","contents_url":"https://api.github.com/repos/danliris/bateeq-module/contents/{+path}","compare_url":"https://api.github.com/repos/danliris/bateeq-module/compare/{base}...{head}","merges_url":"https://api.github.com/repos/danliris/bateeq-module/merges","archive_url":"https://api.github.com/repos/danliris/bateeq-module/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/danliris/bateeq-module/downloads","issues_url":"https://api.github.com/repos/danliris/bateeq-module/issues{/number}","pulls_url":"https://api.github.com/repos/danliris/bateeq-module/pulls{/number}","milestones_url":"https://api.github.com/repos/danliris/bateeq-module/milestones{/number}","notifications_url":"https://api.github.com/repos/danliris/bateeq-module/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/danliris/bateeq-module/labels{/name}","releases_url":"https://api.github.com/repos/danliris/bateeq-module/releases{/id}","deployments_url":"https://api.github.com/repos/danliris/bateeq-module/deployments","created_at":"2016-06-24T05:10:07Z","updated_at":"2016-08-08T03:26:08Z","pushed_at":"2017-07-26T07:36:07Z","git_url":"git://github.com/danliris/bateeq-module.git","ssh_url":"git@github.com:danliris/bateeq-module.git","clone_url":"https://github.com/danliris/bateeq-module.git","svn_url":"https://github.com/danliris/bateeq-module","homepage":null,"size":1286,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":2,"forks":16,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"danliris:uat","ref":"uat","sha":"0d76332d0e6a88557bac402ed445971e49d60158","user":{"login":"danliris","id":20855729,"avatar_url":"https://avatars0.githubusercontent.com/u/20855729?v=4","gravatar_id":"","url":"https://api.github.com/users/danliris","html_url":"https://github.com/danliris","followers_url":"https://api.github.com/users/danliris/followers","following_url":"https://api.github.com/users/danliris/following{/other_user}","gists_url":"https://api.github.com/users/danliris/gists{/gist_id}","starred_url":"https://api.github.com/users/danliris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danliris/subscriptions","organizations_url":"https://api.github.com/users/danliris/orgs","repos_url":"https://api.github.com/users/danliris/repos","events_url":"https://api.github.com/users/danliris/events{/privacy}","received_events_url":"https://api.github.com/users/danliris/received_events","type":"User","site_admin":false},"repo":{"id":61858352,"name":"bateeq-module","full_name":"danliris/bateeq-module","owner":{"login":"danliris","id":20855729,"avatar_url":"https://avatars0.githubusercontent.com/u/20855729?v=4","gravatar_id":"","url":"https://api.github.com/users/danliris","html_url":"https://github.com/danliris","followers_url":"https://api.github.com/users/danliris/followers","following_url":"https://api.github.com/users/danliris/following{/other_user}","gists_url":"https://api.github.com/users/danliris/gists{/gist_id}","starred_url":"https://api.github.com/users/danliris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danliris/subscriptions","organizations_url":"https://api.github.com/users/danliris/orgs","repos_url":"https://api.github.com/users/danliris/repos","events_url":"https://api.github.com/users/danliris/events{/privacy}","received_events_url":"https://api.github.com/users/danliris/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/danliris/bateeq-module","description":"bateeq module","fork":false,"url":"https://api.github.com/repos/danliris/bateeq-module","forks_url":"https://api.github.com/repos/danliris/bateeq-module/forks","keys_url":"https://api.github.com/repos/danliris/bateeq-module/keys{/key_id}","collaborators_url":"https://api.github.com/repos/danliris/bateeq-module/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/danliris/bateeq-module/teams","hooks_url":"https://api.github.com/repos/danliris/bateeq-module/hooks","issue_events_url":"https://api.github.com/repos/danliris/bateeq-module/issues/events{/number}","events_url":"https://api.github.com/repos/danliris/bateeq-module/events","assignees_url":"https://api.github.com/repos/danliris/bateeq-module/assignees{/user}","branches_url":"https://api.github.com/repos/danliris/bateeq-module/branches{/branch}","tags_url":"https://api.github.com/repos/danliris/bateeq-module/tags","blobs_url":"https://api.github.com/repos/danliris/bateeq-module/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/danliris/bateeq-module/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/danliris/bateeq-module/git/refs{/sha}","trees_url":"https://api.github.com/repos/danliris/bateeq-module/git/trees{/sha}","statuses_url":"https://api.github.com/repos/danliris/bateeq-module/statuses/{sha}","languages_url":"https://api.github.com/repos/danliris/bateeq-module/languages","stargazers_url":"https://api.github.com/repos/danliris/bateeq-module/stargazers","contributors_url":"https://api.github.com/repos/danliris/bateeq-module/contributors","subscribers_url":"https://api.github.com/repos/danliris/bateeq-module/subscribers","subscription_url":"https://api.github.com/repos/danliris/bateeq-module/subscription","commits_url":"https://api.github.com/repos/danliris/bateeq-module/commits{/sha}","git_commits_url":"https://api.github.com/repos/danliris/bateeq-module/git/commits{/sha}","comments_url":"https://api.github.com/repos/danliris/bateeq-module/comments{/number}","issue_comment_url":"https://api.github.com/repos/danliris/bateeq-module/issues/comments{/number}","contents_url":"https://api.github.com/repos/danliris/bateeq-module/contents/{+path}","compare_url":"https://api.github.com/repos/danliris/bateeq-module/compare/{base}...{head}","merges_url":"https://api.github.com/repos/danliris/bateeq-module/merges","archive_url":"https://api.github.com/repos/danliris/bateeq-module/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/danliris/bateeq-module/downloads","issues_url":"https://api.github.com/repos/danliris/bateeq-module/issues{/number}","pulls_url":"https://api.github.com/repos/danliris/bateeq-module/pulls{/number}","milestones_url":"https://api.github.com/repos/danliris/bateeq-module/milestones{/number}","notifications_url":"https://api.github.com/repos/danliris/bateeq-module/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/danliris/bateeq-module/labels{/name}","releases_url":"https://api.github.com/repos/danliris/bateeq-module/releases{/id}","deployments_url":"https://api.github.com/repos/danliris/bateeq-module/deployments","created_at":"2016-06-24T05:10:07Z","updated_at":"2016-08-08T03:26:08Z","pushed_at":"2017-07-26T07:36:07Z","git_url":"git://github.com/danliris/bateeq-module.git","ssh_url":"git@github.com:danliris/bateeq-module.git","clone_url":"https://github.com/danliris/bateeq-module.git","svn_url":"https://github.com/danliris/bateeq-module","homepage":null,"size":1286,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":2,"forks":16,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/danliris/bateeq-module/pulls/341"},"html":{"href":"https://github.com/danliris/bateeq-module/pull/341"},"issue":{"href":"https://api.github.com/repos/danliris/bateeq-module/issues/341"},"comments":{"href":"https://api.github.com/repos/danliris/bateeq-module/issues/341/comments"},"review_comments":{"href":"https://api.github.com/repos/danliris/bateeq-module/pulls/341/comments"},"review_comment":{"href":"https://api.github.com/repos/danliris/bateeq-module/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/danliris/bateeq-module/pulls/341/commits"},"statuses":{"href":"https://api.github.com/repos/danliris/bateeq-module/statuses/50634079a9f8ea9776bfd65cbfdea4c9ab4aabc8"}}}}
{"actor":{"display_login":"houndci-bot"}}