Skip to content

Commit

Permalink
feat: Set sourceAccount to null on the io.cozy.contacts.accounts when…
Browse files Browse the repository at this point in the history
… the io.cozy.accounts is disconnected
  • Loading branch information
cedricmessiant committed Mar 13, 2019
1 parent c85155e commit 7cdc057
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
3 changes: 2 additions & 1 deletion manifest.konnector
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@
}
}
},
"manifest_version": "2"
"manifest_version": "2",
"on_delete_account": "onDeleteAccount.js"
}
9 changes: 9 additions & 0 deletions src/CozyUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ class CozyUtils {
return get(resp, 'data.0')
}

async findContactAccount(accountId) {
const accountsCollection = this.client.collection(DOCTYPE_CONTACTS_ACCOUNT)
const result = await accountsCollection.find({
sourceAccount: accountId
})

return get(result.data, 0, null)
}

async findOrCreateContactAccount(accountId, accountEmail) {
const accountsCollection = this.client.collection(DOCTYPE_CONTACTS_ACCOUNT)
const accountsWithSourceAccount = await accountsCollection.find({
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/getAccountId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function getAccountId() {
try {
return process.env.NODE_ENV === 'development'
? 'fakeAccountId'
: JSON.parse(process.env.COZY_FIELDS).account
} catch (err) {
throw new Error(`You must provide 'account' in COZY_FIELDS: ${err.message}`)
}
}

module.exports = getAccountId
11 changes: 1 addition & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@ const {

const CozyUtils = require('./CozyUtils')
const GoogleUtils = require('./GoogleUtils')
const getAccountId = require('./helpers/getAccountId')
const synchronizeContacts = require('./synchronizeContacts')

function getAccountId() {
try {
return process.env.NODE_ENV === 'development'
? 'fakeAccountId'
: JSON.parse(process.env.COZY_FIELDS).account
} catch (err) {
throw new Error(`You must provide 'account' in COZY_FIELDS: ${err.message}`)
}
}

module.exports = new BaseKonnector(start)

/**
Expand Down
30 changes: 30 additions & 0 deletions src/onDeleteAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { log } = require('cozy-konnector-libs')

const getAccountId = require('./helpers/getAccountId')
const CozyUtils = require('./CozyUtils')

async function onDeleteAccount() {
log('info', 'onDeleteAccount: remove account id from the contact account')
const accountId = getAccountId()
const cozyUtils = new CozyUtils(accountId)
const contactAccount = await cozyUtils.findContactAccount(accountId)
contactAccount.sourceAccount = null
await cozyUtils.client.save(contactAccount)
}

onDeleteAccount().then(
() => {
log(
'info',
'onDeleteAccount: Successfully marked the io.cozy.contacts.accounts as inactive'
)
},
err => {
log(
'error',
`onDeleteAccount: An error occured during onDeleteAccount script: ${
err.message
}`
)
}
)
9 changes: 6 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const CopyPlugin = require('copy-webpack-plugin')
const fs = require('fs')
const SvgoInstance = require('svgo')

const entry = require('./package.json').main
const index = require('./package.json').main

const svgo = new SvgoInstance()

Expand All @@ -18,12 +18,15 @@ try {
const appIconRX = iconName && new RegExp(`[^/]*/${iconName}`)

module.exports = {
entry,
entry: {
index,
onDeleteAccount: './src/onDeleteAccount.js'
},
target: 'node',
mode: 'none',
output: {
path: path.join(__dirname, 'build'),
filename: 'index.js'
filename: '[name].js'
},
plugins: [
new CopyPlugin([
Expand Down

0 comments on commit 7cdc057

Please sign in to comment.