{"action":"created","comment":{"url":"https://api.github.com/repos/erxes/erxes-app-api/pulls/comments/143320905","pull_request_review_id":67823296,"id":143320905,"diff_hunk":"@@ -1,35 +1,146 @@\n import mongoose from 'mongoose';\n-import Random from 'meteor-random';\n+import shortid from 'shortid';\n+import Integrations from './Integrations';\n \n const FormSchema = mongoose.Schema({\n   _id: {\n     type: String,\n-    unique: true,\n-    default: () => Random.id(),\n+    default: shortid.generate,\n   },\n   title: String,\n-  code: String,\n   description: String,\n+  code: String,\n   createdUserId: String,\n   createdDate: Date,\n });\n \n+class Form {\n+  static async generateCode() {\n+    // generate code automatically\n+    let code = shortid.generate().substr(0, 6);\n+    let foundForm = await Forms.findOne({ code });\n+\n+    while (foundForm) {\n+      code = shortid.generate().substr(0, 6);\n+      foundForm = await Forms.findOne({ code });\n+    }\n+\n+    return code;\n+  }\n+\n+  static async createForm(doc) {\n+    const { createdUserId } = doc;\n+\n+    if (!createdUserId) {\n+      throw 'createdUserId must be supplied';\n+    }\n+\n+    doc.code = await this.generateCode();\n+    doc.createdDate = new Date();\n+\n+    return this.create(doc);\n+  }\n+\n+  static updateForm(_id, doc) {\n+    return this.update({ _id }, doc);\n+  }\n+\n+  static async removeForm(_id) {\n+    const fieldCount = await FormFields.find({ formId: _id }).count();\n+\n+    if (fieldCount > 0) {\n+      throw 'You cannot delete this form. This form has some fields.';\n+    }\n+\n+    const integrationCount = await Integrations.find({ formId: _id }).count();\n+\n+    if (integrationCount > 0) {\n+      throw 'You cannot delete this form. This form used in integration.';\n+    }\n+\n+    return this.remove({ _id });\n+  }\n+\n+  static async updateFormFieldsOrder(orderDics) {\n+    // update each field's order\n+    for (let orderDic of orderDics) {\n+      await FormFields.updateFormField(orderDic.id, { order: orderDic.order });\n+    }\n+  }\n+\n+  static async duplicate(_id) {\n+    const form = await this.findOne({ _id });\n+\n+    // duplicate form\n+    const newForm = await this.createForm({\n+      title: `${form.title} duplicated`,\n+      description: form.description,\n+      createdUserId: form.createdUserId,\n+    });\n+\n+    // duplicate fields\n+    const formFields = await FormFields.find({ formId: _id });\n+\n+    for (let field of formFields) {\n+      await FormFields.createFormField(newForm._id, {\n+        type: field.type,\n+        validation: field.validation,\n+        text: field.text,\n+        description: field.description,\n+        options: field.options,\n+        isRequired: field.isRequired,\n+        order: field.order,\n+      });\n+    }\n+\n+    return newForm;\n+  }\n+}\n+\n+FormSchema.loadClass(Form);\n export const Forms = mongoose.model('forms', FormSchema);\n \n-const FieldSchema = mongoose.Schema({\n+const FormFieldSchema = mongoose.Schema({\n   _id: {\n     type: String,\n-    unique: true,\n-    default: () => Random.id(),\n+    default: shortid.generate,\n   },\n-  formId: String,\n   type: String,\n   validation: String,\n   text: String,\n   description: String,\n   options: [String],\n   isRequired: Boolean,\n+  formId: {\n+    required: true,\n+    type: String,\n+  },\n   order: Number,\n });\n \n-export const FormFields = mongoose.model('form_fields', FieldSchema);\n+class FormField {\n+  static async createFormField(formId, doc) {\n+    const lastField = await FormFields.findOne({}, { order: 1 }, { sort: { order: -1 } });\n+    doc.formId = formId;\n+\n+    // if there is no field then start with 0\n+    let order = 0;\n+    if (lastField) {","path":"src/db/models/Forms.js","position":137,"original_position":137,"commit_id":"3174c133fb22521be50b4dccc6d80b5403f299aa","original_commit_id":"3174c133fb22521be50b4dccc6d80b5403f299aa","user":{"login":"batamar","id":1748899,"avatar_url":"https://avatars1.githubusercontent.com/u/1748899?v=4","gravatar_id":"","url":"https://api.github.com/users/batamar","html_url":"https://github.com/batamar","followers_url":"https://api.github.com/users/batamar/followers","following_url":"https://api.github.com/users/batamar/following{/other_user}","gists_url":"https://api.github.com/users/batamar/gists{/gist_id}","starred_url":"https://api.github.com/users/batamar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/batamar/subscriptions","organizations_url":"https://api.github.com/users/batamar/orgs","repos_url":"https://api.github.com/users/batamar/repos","events_url":"https://api.github.com/users/batamar/events{/privacy}","received_events_url":"https://api.github.com/users/batamar/received_events","type":"User","site_admin":false},"body":"New line above and below","created_at":"2017-10-07T02:59:04Z","updated_at":"2017-10-07T03:03:32Z","html_url":"https://github.com/erxes/erxes-app-api/pull/13#discussion_r143320905","pull_request_url":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/erxes/erxes-app-api/pulls/comments/143320905"},"html":{"href":"https://github.com/erxes/erxes-app-api/pull/13#discussion_r143320905"},"pull_request":{"href":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13"}}},"pull_request":{"url":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13","id":145260062,"html_url":"https://github.com/erxes/erxes-app-api/pull/13","diff_url":"https://github.com/erxes/erxes-app-api/pull/13.diff","patch_url":"https://github.com/erxes/erxes-app-api/pull/13.patch","issue_url":"https://api.github.com/repos/erxes/erxes-app-api/issues/13","number":13,"state":"open","locked":false,"title":"Graphql mutations ja","user":{"login":"JavkhlanShirendev","id":1748982,"avatar_url":"https://avatars1.githubusercontent.com/u/1748982?v=4","gravatar_id":"","url":"https://api.github.com/users/JavkhlanShirendev","html_url":"https://github.com/JavkhlanShirendev","followers_url":"https://api.github.com/users/JavkhlanShirendev/followers","following_url":"https://api.github.com/users/JavkhlanShirendev/following{/other_user}","gists_url":"https://api.github.com/users/JavkhlanShirendev/gists{/gist_id}","starred_url":"https://api.github.com/users/JavkhlanShirendev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JavkhlanShirendev/subscriptions","organizations_url":"https://api.github.com/users/JavkhlanShirendev/orgs","repos_url":"https://api.github.com/users/JavkhlanShirendev/repos","events_url":"https://api.github.com/users/JavkhlanShirendev/events{/privacy}","received_events_url":"https://api.github.com/users/JavkhlanShirendev/received_events","type":"User","site_admin":false},"body":"`sendNotifications` method on channels is not yet implemented.\r\nPlease review the latest changes.\r\nThere is some weird occasional 1, 2 test fails every 2nd or 3rd time i run the tests related to async and await inside model static methods. (for example `Form.duplicate`)","created_at":"2017-10-06T22:43:52Z","updated_at":"2017-10-07T03:03:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7d394bb52922a917849f5eea63ea7265312b15ec","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13/commits","review_comments_url":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13/comments","review_comment_url":"https://api.github.com/repos/erxes/erxes-app-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/erxes/erxes-app-api/issues/13/comments","statuses_url":"https://api.github.com/repos/erxes/erxes-app-api/statuses/3174c133fb22521be50b4dccc6d80b5403f299aa","head":{"label":"erxes:graphql-mutations-ja","ref":"graphql-mutations-ja","sha":"3174c133fb22521be50b4dccc6d80b5403f299aa","user":{"login":"erxes","id":20272096,"avatar_url":"https://avatars2.githubusercontent.com/u/20272096?v=4","gravatar_id":"","url":"https://api.github.com/users/erxes","html_url":"https://github.com/erxes","followers_url":"https://api.github.com/users/erxes/followers","following_url":"https://api.github.com/users/erxes/following{/other_user}","gists_url":"https://api.github.com/users/erxes/gists{/gist_id}","starred_url":"https://api.github.com/users/erxes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erxes/subscriptions","organizations_url":"https://api.github.com/users/erxes/orgs","repos_url":"https://api.github.com/users/erxes/repos","events_url":"https://api.github.com/users/erxes/events{/privacy}","received_events_url":"https://api.github.com/users/erxes/received_events","type":"Organization","site_admin":false},"repo":{"id":102176438,"name":"erxes-app-api","full_name":"erxes/erxes-app-api","owner":{"login":"erxes","id":20272096,"avatar_url":"https://avatars2.githubusercontent.com/u/20272096?v=4","gravatar_id":"","url":"https://api.github.com/users/erxes","html_url":"https://github.com/erxes","followers_url":"https://api.github.com/users/erxes/followers","following_url":"https://api.github.com/users/erxes/following{/other_user}","gists_url":"https://api.github.com/users/erxes/gists{/gist_id}","starred_url":"https://api.github.com/users/erxes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erxes/subscriptions","organizations_url":"https://api.github.com/users/erxes/orgs","repos_url":"https://api.github.com/users/erxes/repos","events_url":"https://api.github.com/users/erxes/events{/privacy}","received_events_url":"https://api.github.com/users/erxes/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/erxes/erxes-app-api","description":"API for erxes administration apps","fork":false,"url":"https://api.github.com/repos/erxes/erxes-app-api","forks_url":"https://api.github.com/repos/erxes/erxes-app-api/forks","keys_url":"https://api.github.com/repos/erxes/erxes-app-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/erxes/erxes-app-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/erxes/erxes-app-api/teams","hooks_url":"https://api.github.com/repos/erxes/erxes-app-api/hooks","issue_events_url":"https://api.github.com/repos/erxes/erxes-app-api/issues/events{/number}","events_url":"https://api.github.com/repos/erxes/erxes-app-api/events","assignees_url":"https://api.github.com/repos/erxes/erxes-app-api/assignees{/user}","branches_url":"https://api.github.com/repos/erxes/erxes-app-api/branches{/branch}","tags_url":"https://api.github.com/repos/erxes/erxes-app-api/tags","blobs_url":"https://api.github.com/repos/erxes/erxes-app-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/erxes/erxes-app-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/erxes/erxes-app-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/erxes/erxes-app-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/erxes/erxes-app-api/statuses/{sha}","languages_url":"https://api.github.com/repos/erxes/erxes-app-api/languages","stargazers_url":"https://api.github.com/repos/erxes/erxes-app-api/stargazers","contributors_url":"https://api.github.com/repos/erxes/erxes-app-api/contributors","subscribers_url":"https://api.github.com/repos/erxes/erxes-app-api/subscribers","subscription_url":"https://api.github.com/repos/erxes/erxes-app-api/subscription","commits_url":"https://api.github.com/repos/erxes/erxes-app-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/erxes/erxes-app-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/erxes/erxes-app-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/erxes/erxes-app-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/erxes/erxes-app-api/contents/{+path}","compare_url":"https://api.github.com/repos/erxes/erxes-app-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/erxes/erxes-app-api/merges","archive_url":"https://api.github.com/repos/erxes/erxes-app-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/erxes/erxes-app-api/downloads","issues_url":"https://api.github.com/repos/erxes/erxes-app-api/issues{/number}","pulls_url":"https://api.github.com/repos/erxes/erxes-app-api/pulls{/number}","milestones_url":"https://api.github.com/repos/erxes/erxes-app-api/milestones{/number}","notifications_url":"https://api.github.com/repos/erxes/erxes-app-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/erxes/erxes-app-api/labels{/name}","releases_url":"https://api.github.com/repos/erxes/erxes-app-api/releases{/id}","deployments_url":"https://api.github.com/repos/erxes/erxes-app-api/deployments","created_at":"2017-09-02T06:00:44Z","updated_at":"2017-09-25T15:33:32Z","pushed_at":"2017-10-06T22:43:52Z","git_url":"git://github.com/erxes/erxes-app-api.git","ssh_url":"git@github.com:erxes/erxes-app-api.git","clone_url":"https://github.com/erxes/erxes-app-api.git","svn_url":"https://github.com/erxes/erxes-app-api","homepage":null,"size":320,"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":0,"mirror_url":null,"open_issues_count":6,"forks":0,"open_issues":6,"watchers":0,"default_branch":"master"}},"base":{"label":"erxes:feature-graphql-mutations","ref":"feature-graphql-mutations","sha":"5bfe494b8e4cd384a51468b8a1dfb67e8e058db4","user":{"login":"erxes","id":20272096,"avatar_url":"https://avatars2.githubusercontent.com/u/20272096?v=4","gravatar_id":"","url":"https://api.github.com/users/erxes","html_url":"https://github.com/erxes","followers_url":"https://api.github.com/users/erxes/followers","following_url":"https://api.github.com/users/erxes/following{/other_user}","gists_url":"https://api.github.com/users/erxes/gists{/gist_id}","starred_url":"https://api.github.com/users/erxes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erxes/subscriptions","organizations_url":"https://api.github.com/users/erxes/orgs","repos_url":"https://api.github.com/users/erxes/repos","events_url":"https://api.github.com/users/erxes/events{/privacy}","received_events_url":"https://api.github.com/users/erxes/received_events","type":"Organization","site_admin":false},"repo":{"id":102176438,"name":"erxes-app-api","full_name":"erxes/erxes-app-api","owner":{"login":"erxes","id":20272096,"avatar_url":"https://avatars2.githubusercontent.com/u/20272096?v=4","gravatar_id":"","url":"https://api.github.com/users/erxes","html_url":"https://github.com/erxes","followers_url":"https://api.github.com/users/erxes/followers","following_url":"https://api.github.com/users/erxes/following{/other_user}","gists_url":"https://api.github.com/users/erxes/gists{/gist_id}","starred_url":"https://api.github.com/users/erxes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erxes/subscriptions","organizations_url":"https://api.github.com/users/erxes/orgs","repos_url":"https://api.github.com/users/erxes/repos","events_url":"https://api.github.com/users/erxes/events{/privacy}","received_events_url":"https://api.github.com/users/erxes/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/erxes/erxes-app-api","description":"API for erxes administration apps","fork":false,"url":"https://api.github.com/repos/erxes/erxes-app-api","forks_url":"https://api.github.com/repos/erxes/erxes-app-api/forks","keys_url":"https://api.github.com/repos/erxes/erxes-app-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/erxes/erxes-app-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/erxes/erxes-app-api/teams","hooks_url":"https://api.github.com/repos/erxes/erxes-app-api/hooks","issue_events_url":"https://api.github.com/repos/erxes/erxes-app-api/issues/events{/number}","events_url":"https://api.github.com/repos/erxes/erxes-app-api/events","assignees_url":"https://api.github.com/repos/erxes/erxes-app-api/assignees{/user}","branches_url":"https://api.github.com/repos/erxes/erxes-app-api/branches{/branch}","tags_url":"https://api.github.com/repos/erxes/erxes-app-api/tags","blobs_url":"https://api.github.com/repos/erxes/erxes-app-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/erxes/erxes-app-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/erxes/erxes-app-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/erxes/erxes-app-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/erxes/erxes-app-api/statuses/{sha}","languages_url":"https://api.github.com/repos/erxes/erxes-app-api/languages","stargazers_url":"https://api.github.com/repos/erxes/erxes-app-api/stargazers","contributors_url":"https://api.github.com/repos/erxes/erxes-app-api/contributors","subscribers_url":"https://api.github.com/repos/erxes/erxes-app-api/subscribers","subscription_url":"https://api.github.com/repos/erxes/erxes-app-api/subscription","commits_url":"https://api.github.com/repos/erxes/erxes-app-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/erxes/erxes-app-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/erxes/erxes-app-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/erxes/erxes-app-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/erxes/erxes-app-api/contents/{+path}","compare_url":"https://api.github.com/repos/erxes/erxes-app-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/erxes/erxes-app-api/merges","archive_url":"https://api.github.com/repos/erxes/erxes-app-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/erxes/erxes-app-api/downloads","issues_url":"https://api.github.com/repos/erxes/erxes-app-api/issues{/number}","pulls_url":"https://api.github.com/repos/erxes/erxes-app-api/pulls{/number}","milestones_url":"https://api.github.com/repos/erxes/erxes-app-api/milestones{/number}","notifications_url":"https://api.github.com/repos/erxes/erxes-app-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/erxes/erxes-app-api/labels{/name}","releases_url":"https://api.github.com/repos/erxes/erxes-app-api/releases{/id}","deployments_url":"https://api.github.com/repos/erxes/erxes-app-api/deployments","created_at":"2017-09-02T06:00:44Z","updated_at":"2017-09-25T15:33:32Z","pushed_at":"2017-10-06T22:43:52Z","git_url":"git://github.com/erxes/erxes-app-api.git","ssh_url":"git@github.com:erxes/erxes-app-api.git","clone_url":"https://github.com/erxes/erxes-app-api.git","svn_url":"https://github.com/erxes/erxes-app-api","homepage":null,"size":320,"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":0,"mirror_url":null,"open_issues_count":6,"forks":0,"open_issues":6,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13"},"html":{"href":"https://github.com/erxes/erxes-app-api/pull/13"},"issue":{"href":"https://api.github.com/repos/erxes/erxes-app-api/issues/13"},"comments":{"href":"https://api.github.com/repos/erxes/erxes-app-api/issues/13/comments"},"review_comments":{"href":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13/comments"},"review_comment":{"href":"https://api.github.com/repos/erxes/erxes-app-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/erxes/erxes-app-api/pulls/13/commits"},"statuses":{"href":"https://api.github.com/repos/erxes/erxes-app-api/statuses/3174c133fb22521be50b4dccc6d80b5403f299aa"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"batamar"}}