Skip to content

Commit

Permalink
fix(import): import xlsx get wrong json issue
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanlvr authored and ysfscream committed Oct 9, 2021
1 parent 993eaf6 commit d3fc9e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/components/ImportData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,16 @@ export default class ImportData extends Vue {
}
const parseStringProps = (connection: any): ConnectionModel => {
let { client, messages, will, subscriptions } = connection
let { messages, will, subscriptions } = connection
try {
client = JSON.parse(client)
messages = JSON.parse(messages)
will = JSON.parse(will)
subscriptions = JSON.parse(subscriptions)
} catch (err) {
this.$message.error(err.toString())
caughtError = true
}
return Object.assign(connection, { client, messages, will, subscriptions })
return Object.assign(connection, { messages, will, subscriptions })
}
const jsonContent = content.map((connection): ConnectionModel => parseStringProps(connection))
if (!caughtError) {
Expand Down
32 changes: 21 additions & 11 deletions src/database/services/ConnectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,33 @@ export default class ConnectionService {
.createQueryBuilder('sub')
.where('sub.connectionId = :id', { id })
.getMany()
await this.subscriptionRepository.remove(
curSubs.filter(
(subInDataBase) => !res.subscriptions.some((subInMemory) => subInMemory.id === subInDataBase.id),
),
const shouldRemove: SubscriptionEntity[] = curSubs.filter(
(subInDataBase) => !res.subscriptions.some((subInMemory) => subInMemory.id === subInDataBase.id),
)
if (res.subscriptions.length) {
res.subscriptions = await this.subscriptionRepository.save(
res.subscriptions.map((sub) => {
const shouldUpdate: SubscriptionEntity[] = res.subscriptions.filter((subInMemory) =>
res.subscriptions.some((subInDataBase) => subInMemory.id === subInDataBase.id),
)
await this.subscriptionRepository.remove(shouldRemove)
if (shouldUpdate && shouldUpdate.length) {
res.subscriptions = (await this.subscriptionRepository.save(
shouldUpdate.map((sub) => {
return {
...sub,
connectionId: id,
connectionId: undefined,
} as SubscriptionEntity
}),
)
)) as SubscriptionEntity[]
}
}
// TODO: too large cost for message saving, need to refactor
if (res.messages && Array.isArray(res.messages) && res.messages.length) {
res.messages = await this.messageRepository.save(res.messages)
const shouldSave: MessageEntity[] = res.messages.map((msg) => {
return {
...msg,
connectionId: undefined,
} as MessageEntity
})
res.messages = await this.messageRepository.save(shouldSave)
}
const saved: ConnectionEntity | undefined = await this.connectionRepository.save({
...res,
Expand Down Expand Up @@ -360,6 +368,8 @@ export default class ConnectionService {
await this.subscriptionRepository.remove(
query.filter((subInDb) => !subs.some((subInMemory) => subInMemory.id === subInDb.id)),
)
await this.subscriptionRepository.save(subs)
await this.subscriptionRepository.save(
subs.filter((subInMemory) => query.some((subInDb) => subInMemory.id === subInDb.id)),
)
}
}

0 comments on commit d3fc9e2

Please sign in to comment.