ConvertAccount.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <main>
  3. <h1>Convert account</h1>
  4. <hr/>
  5. <br/>
  6. <account-form class="account-form" v-if="oldAccount.version" :onSubmit="() => {}" :initialAccount="oldAccount" :readonly="true"/>
  7. <account-form class="account-form" v-if="newAccount.version" :onSubmit="onSubmit" :initialAccount="newAccount"/>
  8. <p v-else>{{ message }}</p>
  9. </main>
  10. </template>
  11. <script>
  12. import AccountForm from '../components/AccountForm.vue';
  13. import io from "../../io.js";
  14. export default {
  15. components: { AccountForm },
  16. data: () => {
  17. return {
  18. oldAccount: {},
  19. newAccount: {},
  20. accountId: "",
  21. message: ""
  22. }
  23. },
  24. methods: {
  25. onSubmit(account) {
  26. console.log(account);
  27. this.socket.emit("account.editById", this.oldAccount._id, account, (res) => {
  28. console.log(res);
  29. if (res.status === "success") {
  30. this.$router.push("/accounts")
  31. }
  32. });
  33. },
  34. createMigratingAccount() {
  35. /*this.$set(this.newAccount, "fields", {});
  36. this.$set(this.newAccount, "version", this.newSchema.version);
  37. let defaultNewObjects = {};
  38. this.newSchema.fields.forEach(newField => {
  39. const oldField = this.oldSchema.fields.find(oldField => oldField.fieldId === newField.fieldId);
  40. let defaultNewObject = {};
  41. //console.log(newField.fieldId, newField);
  42. newField.fieldTypes.forEach(newFieldType => {
  43. if (newFieldType.type === "text" || newFieldType.type === "select") defaultNewObject[newFieldType.fieldTypeId] = "";
  44. else if (newFieldType.type === "checkbox") defaultNewObject[newFieldType.fieldTypeId] = false;
  45. });
  46. defaultNewObjects[newField.fieldId] = defaultNewObject;
  47. this.$set(this.newAccount.fields, newField.fieldId, []);
  48. if (oldField) { // If the new field id is the same in the old & new schema
  49. // console.log("FIELD STILL EXISTS", newField.fieldId);
  50. let entries = [];
  51. this.oldAccount.fields[oldField.fieldId].forEach(oldAccountFieldEntry => {
  52. entries.push({});
  53. });
  54. newField.fieldTypes.forEach(newFieldType => {
  55. const oldFieldType = oldField.fieldTypes.find(oldFieldType => oldFieldType.fieldTypeId === newFieldType.fieldTypeId);
  56. if (oldFieldType) { // If the new field type id is the same in the old & new schema
  57. // console.log("FIELDTYPE STILL EXISTS", newFieldType.fieldTypeId);
  58. this.oldAccount.fields[oldField.fieldId].forEach((oldAccountFieldEntry, index) => {
  59. entries[index][newFieldType.fieldTypeId] = oldAccountFieldEntry[newFieldType.fieldTypeId];
  60. });
  61. } else { // If the new field type id was not in the old schema
  62. // console.log("NEW FIELDTYPE", newFieldType.fieldTypeId);
  63. entries = entries.map(entry => {
  64. entry[newFieldType.fieldTypeId] = defaultNewObject[newFieldType.fieldTypeId];
  65. return entry;
  66. });
  67. }
  68. });
  69. this.$set(this.newAccount.fields, newField.fieldId, entries);
  70. }
  71. });
  72. Object.keys(this.migrate.changes).forEach(changeOld => {
  73. const oldFieldId = changeOld.split(".")[0];
  74. const oldFieldTypeId = changeOld.split(".")[1];
  75. const changeNew = this.migrate.changes[changeOld];
  76. const newFieldId = changeNew.split(".")[0];
  77. const newFieldTypeId = changeNew.split(".")[1];
  78. const oldField = this.oldAccount.fields[oldFieldId];
  79. const newField = this.newAccount.fields[newFieldId];
  80. //console.log(oldField, newField);
  81. const entriesToAdd = oldField.length - newField.length;
  82. for(let i = 0; i < entriesToAdd; i++) {
  83. this.newAccount.fields[newFieldId].push(JSON.parse(JSON.stringify(defaultNewObjects[newFieldId])));
  84. }
  85. for(let i = 0; i < newField.length; i++) {
  86. //console.log(i, this.oldAccount.fields[oldFieldId][i][oldFieldTypeId], this.newAccount.fields[newFieldId][i][newFieldTypeId]);
  87. //this.$set(this.newAccount.fields[newFieldId][i], `${newFieldTypeId}`, this.oldAccount.fields[oldFieldId][i][oldFieldTypeId]);
  88. this.$set(this.newAccount.fields[newFieldId][i], newFieldTypeId, this.oldAccount.fields[oldFieldId][i][oldFieldTypeId]);
  89. //console.log(this.newAccount.fields[newFieldId][i]);
  90. }
  91. });
  92. this.newSchema.fields.forEach(newField => {
  93. const entriesToAdd = newField.minEntries - this.newAccount.fields[newField.fieldId];
  94. for(let i = 0; i < entriesToAdd; i++) {
  95. this.newAccount.fields[newField.fieldId].push(JSON.parse(JSON.stringify(defaultNewObjects[newField.fieldId])));
  96. }
  97. });
  98. //console.log(defaultNewObjects["newemail"]);*/
  99. }
  100. },
  101. mounted() {
  102. this.accountId = this.$route.params.accountId;
  103. io.getSocket(socket => {
  104. this.socket = socket;
  105. this.socket.emit("account.getById", this.accountId, res => {
  106. console.log(res);
  107. if (res.status === "success") {
  108. this.oldAccount = res.account;
  109. }
  110. });
  111. this.socket.emit("account.getMigratedAccount", this.accountId, res => {
  112. console.log(res);
  113. if (res.status === "success") {
  114. this.newAccount = res.account;
  115. } else this.message = res.message;
  116. });
  117. /*this.socket.emit("accountSchema.getByVersion", this.migrate.versionFrom, res => {
  118. this.oldSchema = res.schema;
  119. this.socket.emit("accountSchema.getByVersion", this.migrate.versionTo, res2 => {
  120. this.newSchema = res2.schema;
  121. this.createMigratingAccount();
  122. });
  123. });*/
  124. }); }
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .account-form {
  129. float: left;
  130. &:last-of-type {
  131. margin-left: 32px;
  132. }
  133. }
  134. </style>