diff --git a/seeders/20260318200321-test-user.js b/seeders/20260318200321-test-user.js deleted file mode 100644 index 33a311c..0000000 --- a/seeders/20260318200321-test-user.js +++ /dev/null @@ -1,92 +0,0 @@ -'use strict'; - -const { v4 } = require('uuid'); -const { Op } = require('sequelize'); - -const domain = { - id: 'a1b2c3d4-0000-0000-0000-000000000001', - domain: 'internxt.eu', - status: 'active', - created_at: new Date(), - updated_at: new Date(), -}; - -const account = { - id: 'a1b2c3d4-0000-0000-0000-000000000002', - // Matches the uuid of the test user in drive-server-wip seeders - user_id: '87204d6b-c4a7-4f38-bd99-f7f47964a643', - created_at: new Date(), - updated_at: new Date(), -}; - -const address = { - id: 'a1b2c3d4-0000-0000-0000-000000000003', - mail_account_id: account.id, - address: 'john@internxt.eu', - domain_id: domain.id, - is_default: true, - created_at: new Date(), - updated_at: new Date(), -}; - -const providerAccount = { - id: 'a1b2c3d4-0000-0000-0000-000000000004', - mail_address_id: address.id, - provider: 'stalwart', - external_id: address.address, - created_at: new Date(), - updated_at: new Date(), -}; - -module.exports = { - async up(queryInterface) { - const [existingDomains] = await queryInterface.sequelize.query( - 'SELECT id FROM mail_domains WHERE id = :id', - { replacements: { id: domain.id }, type: queryInterface.sequelize.QueryTypes.SELECT }, - ); - if (!existingDomains) { - await queryInterface.bulkInsert('mail_domains', [domain]); - } - - const [existingAccount] = await queryInterface.sequelize.query( - 'SELECT id FROM mail_accounts WHERE user_id = :uuid', - { replacements: { uuid: account.user_id }, type: queryInterface.sequelize.QueryTypes.SELECT }, - ); - if (!existingAccount) { - await queryInterface.bulkInsert('mail_accounts', [account]); - } - - const [existingAddress] = await queryInterface.sequelize.query( - 'SELECT id FROM mail_addresses WHERE address = :address', - { replacements: { address: address.address }, type: queryInterface.sequelize.QueryTypes.SELECT }, - ); - if (!existingAddress) { - await queryInterface.bulkInsert('mail_addresses', [address]); - } - - const [existingProviderAccount] = await queryInterface.sequelize.query( - 'SELECT id FROM mail_provider_accounts WHERE mail_address_id = :id', - { replacements: { id: address.id }, type: queryInterface.sequelize.QueryTypes.SELECT }, - ); - if (!existingProviderAccount) { - await queryInterface.bulkInsert('mail_provider_accounts', [providerAccount]); - } - }, - - async down(queryInterface) { - await queryInterface.bulkDelete('mail_provider_accounts', { - id: { [Op.in]: [providerAccount.id] }, - }); - await queryInterface.bulkDelete('mail_addresses', { - id: { [Op.in]: [address.id] }, - }); - await queryInterface.bulkDelete('mail_accounts', { - id: { [Op.in]: [account.id] }, - }); - await queryInterface.bulkDelete('mail_domains', { - id: { [Op.in]: [domain.id] }, - }); - }, -}; - -module.exports.fixtures = { domain, account, address, providerAccount }; diff --git a/seeders/20260416144215-add-mail-domains.js b/seeders/20260508130610-add-mail-domains.js similarity index 100% rename from seeders/20260416144215-add-mail-domains.js rename to seeders/20260508130610-add-mail-domains.js diff --git a/seeders/20260508130611-test-user.js b/seeders/20260508130611-test-user.js new file mode 100644 index 0000000..7814a7c --- /dev/null +++ b/seeders/20260508130611-test-user.js @@ -0,0 +1,104 @@ +'use strict'; + +const { Op } = require('sequelize'); + +const DOMAIN_NAME = 'inxt.eu'; + +const users = [ + { + accountId: 'a1b2c3d4-0000-0000-0000-000000000002', + userId: '87204d6b-c4a7-4f38-bd99-f7f47964a643', + addressId: 'a1b2c3d4-0000-0000-0000-000000000003', + providerAccountId: 'a1b2c3d4-0000-0000-0000-000000000004', + address: 'john@inxt.eu', + }, + { + accountId: 'a1b2c3d4-0000-0000-0000-000000000012', + userId: 'a1b2c3d4-0000-0000-0000-0000000000a1', + addressId: 'a1b2c3d4-0000-0000-0000-000000000013', + providerAccountId: 'a1b2c3d4-0000-0000-0000-000000000014', + address: 'alice@inxt.eu', + }, + { + accountId: 'a1b2c3d4-0000-0000-0000-000000000022', + userId: 'a1b2c3d4-0000-0000-0000-0000000000b1', + addressId: 'a1b2c3d4-0000-0000-0000-000000000023', + providerAccountId: 'a1b2c3d4-0000-0000-0000-000000000024', + address: 'bob@inxt.eu', + }, +]; + +module.exports = { + async up(queryInterface) { + const [domain] = await queryInterface.sequelize.query( + 'SELECT id FROM mail_domains WHERE domain = :name', + { replacements: { name: DOMAIN_NAME }, type: queryInterface.sequelize.QueryTypes.SELECT }, + ); + if (!domain) { + throw new Error(`Domain ${DOMAIN_NAME} not found — ensure add-mail-domains seeder runs first`); + } + + const now = new Date(); + + for (const u of users) { + const [existingAccount] = await queryInterface.sequelize.query( + 'SELECT id FROM mail_accounts WHERE user_id = :uuid', + { replacements: { uuid: u.userId }, type: queryInterface.sequelize.QueryTypes.SELECT }, + ); + if (!existingAccount) { + await queryInterface.bulkInsert('mail_accounts', [ + { id: u.accountId, user_id: u.userId, created_at: now, updated_at: now }, + ]); + } + + const [existingAddress] = await queryInterface.sequelize.query( + 'SELECT id FROM mail_addresses WHERE address = :address', + { replacements: { address: u.address }, type: queryInterface.sequelize.QueryTypes.SELECT }, + ); + if (!existingAddress) { + await queryInterface.bulkInsert('mail_addresses', [ + { + id: u.addressId, + mail_account_id: u.accountId, + address: u.address, + domain_id: domain.id, + is_default: true, + created_at: now, + updated_at: now, + }, + ]); + } + + const [existingProviderAccount] = await queryInterface.sequelize.query( + 'SELECT id FROM mail_provider_accounts WHERE mail_address_id = :id', + { replacements: { id: u.addressId }, type: queryInterface.sequelize.QueryTypes.SELECT }, + ); + if (!existingProviderAccount) { + await queryInterface.bulkInsert('mail_provider_accounts', [ + { + id: u.providerAccountId, + mail_address_id: u.addressId, + provider: 'stalwart', + external_id: u.address, + created_at: now, + updated_at: now, + }, + ]); + } + } + }, + + async down(queryInterface) { + await queryInterface.bulkDelete('mail_provider_accounts', { + id: { [Op.in]: users.map((u) => u.providerAccountId) }, + }); + await queryInterface.bulkDelete('mail_addresses', { + id: { [Op.in]: users.map((u) => u.addressId) }, + }); + await queryInterface.bulkDelete('mail_accounts', { + id: { [Op.in]: users.map((u) => u.accountId) }, + }); + }, +}; + +module.exports.fixtures = { users };