|
| 1 | +// Invoke with e.g.: node api/utility/chatwoot.js 1234 > chatwoot.log 2>&1 & |
| 2 | +import config from '../../config/config.js'; |
| 3 | +import db from '../helpers/litedb.js'; |
| 4 | + |
| 5 | +export const chatwootSetup = async (tournId = parseInt(process.argv[2])) => { |
| 6 | + if (!tournId || isNaN(tournId)) { |
| 7 | + throw new Error('No tournament ID provided'); |
| 8 | + } |
| 9 | + |
| 10 | + console.log(`Starting chatwoot setup for tournament # ${tournId} ...\n`); |
| 11 | + |
| 12 | + const tourn = await db.sequelize.query(` |
| 13 | + select * from tourn |
| 14 | + where id = ${tournId} |
| 15 | + `, { |
| 16 | + type : db.sequelize.QueryTypes.SELECT, |
| 17 | + }); |
| 18 | + |
| 19 | + if (!tourn || tourn.length === 0) { |
| 20 | + throw new Error('No tournament found'); |
| 21 | + } |
| 22 | + |
| 23 | + const fetchOptions = { |
| 24 | + method: 'POST', |
| 25 | + headers: { |
| 26 | + 'Content-Type': 'application/json', |
| 27 | + 'api_access_token': process.env.CHATWOOT_TOKEN || config.CHATWOOT.TOKEN, |
| 28 | + }, |
| 29 | + }; |
| 30 | + |
| 31 | + const account = { |
| 32 | + name: tourn[0].name, |
| 33 | + locale: 'en', |
| 34 | + domain: `${tourn[0].webname}.chat.tabroom.com`, |
| 35 | + support_email: 'support@tabroom.com', |
| 36 | + status: "active", |
| 37 | + limits: {}, |
| 38 | + custom_attributes: {} |
| 39 | + }; |
| 40 | + |
| 41 | + let accountId = null; |
| 42 | + |
| 43 | + try { |
| 44 | + const accountData = await fetch( |
| 45 | + `${config.CHATWOOT.API_URL}/accounts`, |
| 46 | + { ...fetchOptions, body: JSON.stringify(account) }, |
| 47 | + ); |
| 48 | + const json = await accountData.json(); |
| 49 | + accountId = json.id; |
| 50 | + console.log(json); |
| 51 | + } catch (err) { |
| 52 | + console.error('Error creating chatwoot account:', err); |
| 53 | + } |
| 54 | + |
| 55 | + const users = await db.sequelize.query(` |
| 56 | + select |
| 57 | + person.email, |
| 58 | + CONCAT(person.first, ' ', person.last) as name, |
| 59 | + permission.tag |
| 60 | + from person |
| 61 | + inner join permission on permission.person = person.id |
| 62 | + where permission.tourn = ${tournId} |
| 63 | + and permission.tag IN ('owner', 'tabber') |
| 64 | + `, { |
| 65 | + type : db.sequelize.QueryTypes.SELECT, |
| 66 | + }); |
| 67 | + |
| 68 | + for (const user of users) { |
| 69 | + const userData = { |
| 70 | + name: user.name, |
| 71 | + display_name: user.name, |
| 72 | + email: user.email, |
| 73 | + password: '1234ABCDabcd!@#$', |
| 74 | + custom_attributes: {}, |
| 75 | + }; |
| 76 | + |
| 77 | + let userId = null; |
| 78 | + try { |
| 79 | + const userDataResponse = await fetch( |
| 80 | + `${config.CHATWOOT.API_URL}/users`, |
| 81 | + { ...fetchOptions, body: JSON.stringify(userData) }, |
| 82 | + ); |
| 83 | + const json = await userDataResponse.json(); |
| 84 | + userId = json.id; |
| 85 | + console.log(json); |
| 86 | + } catch (err) { |
| 87 | + console.error(`Error creating chatwoot user for ${user.email}:`, err); |
| 88 | + } |
| 89 | + |
| 90 | + const accountUser = { |
| 91 | + account_id: accountId, |
| 92 | + user_id: userId, |
| 93 | + role: user.tag === 'owner' ? 'administrator' : 'agent', |
| 94 | + }; |
| 95 | + |
| 96 | + try { |
| 97 | + const data = await fetch( |
| 98 | + `${config.CHATWOOT.API_URL}/accounts/${accountId}/account_users`, |
| 99 | + { ...fetchOptions, body: JSON.stringify(accountUser) }, |
| 100 | + ); |
| 101 | + const json = await data.json(); |
| 102 | + console.log(json); |
| 103 | + } catch (err) { |
| 104 | + console.error(`Error creating chatwoot account user for ${user.email}:`, err); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + console.log(`\nDone.`); |
| 109 | +}; |
| 110 | + |
| 111 | +await chatwootSetup(); |
| 112 | +process.exit(); |
| 113 | + |
| 114 | +export default chatwootSetup; |
0 commit comments