Skip to content

Commit 8897b5f

Browse files
author
Pedro Aim
committed
parsing address in what i believe a simpler way
1 parent c20b006 commit 8897b5f

File tree

13 files changed

+128
-83
lines changed

13 files changed

+128
-83
lines changed

electron_app/src/database/DBEmanager.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,23 +1478,37 @@ const updateCustomDomain = ({ id, name, validated, accountId }) => {
14781478
};
14791479

14801480
const getCustomDomainByParams = params => {
1481-
return CustomDomain().findAll({ where: params });
1481+
return CustomDomain()
1482+
.findAll({ where: params })
1483+
.then(domains => {
1484+
return domains.map(domain => domain.toJSON());
1485+
});
14821486
};
14831487

14841488
const deleteCustomDomainByName = ({ name, accountId }) => {
1485-
return CustomDomain().destroy({
1486-
where: {
1487-
name,
1488-
accountId
1489-
}
1489+
return getDB().transaction(async trx => {
1490+
await CustomDomain().destroy({
1491+
where: {
1492+
name,
1493+
accountId
1494+
},
1495+
transaction: trx
1496+
});
1497+
await Alias().destroy({
1498+
where: {
1499+
domain: name,
1500+
accountId
1501+
},
1502+
transaction: trx
1503+
});
14901504
});
14911505
};
14921506

14931507
const deleteCustomDomains = params => {
14941508
const { domain, domains, accountId } = params;
14951509
const theDomains = domain ? domain : domains;
14961510
const whereParam = { name: theDomains, accountId };
1497-
return CustomDomain().destroy({ where: whereParam });
1511+
return deleteCustomDomainByName(whereParam);
14981512
};
14991513

15001514
/* Functions

electron_app/src/ipc/logger.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { ipcMain: ipc } = require('@criptext/electron-better-ipc');
2+
const logger = require('../logger');
3+
4+
ipc.answerRenderer('log-error', stack => {
5+
logger.error(stack);
6+
});

email_mailbox/src/components/AliasesWrapper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class AliasesWrapper extends Component {
4040
}
4141

4242
async componentDidMount() {
43-
const domainsRaw = await getCustomDomainByParams({});
44-
const domains = domainsRaw.map(domain => domain.dataValues.name);
43+
const domainsRaw = await getCustomDomainByParams({ validated: true });
44+
const domains = domainsRaw.map(domain => domain.name);
4545
this.setState({
4646
domains: [appDomain, ...domains]
4747
});

email_mailbox/src/components/CustomDomains.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const Step1 = props => {
6060
type="text"
6161
onChange={e => props.onChangeInputDomain(e)}
6262
className={props.existError ? 'input-error' : ''}
63+
value={props.domain}
6364
/>
6465
{inputError(props)}
6566
</div>
@@ -292,6 +293,7 @@ CustomDomains.propTypes = {
292293
};
293294

294295
Step1.propTypes = {
296+
domain: PropTypes.string,
295297
onChangeInputDomain: PropTypes.func,
296298
existError: PropTypes.bool
297299
};

email_mailbox/src/components/CustomDomainsWrapper.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
createCustomDomain,
1010
updateCustomDomain
1111
} from '../utils/ipc';
12+
import { domainRegex } from '../utils/RegexUtils';
1213

1314
const errors = {
15+
INVALID_FORMAT: string.address.add.step1.errors.invalidFormat,
1416
NOT_DOMAIN_AVAILABLE: string.address.add.step1.errors.usedDomain,
1517
UNKNOWN_DOMAIN_AVAILABLE: string.address.add.step1.errors.unknownError,
1618
FAILED_MX_RECORDS: string.address.add.step2_2.errors.notMXRecords,
@@ -68,7 +70,7 @@ class CustomDomainsWrapper extends Component {
6870

6971
handleInputDomain = ev => {
7072
this.setState({
71-
domain: ev.target.value
73+
domain: ev.target.value.toLowerCase().trim()
7274
});
7375
};
7476

@@ -87,8 +89,17 @@ class CustomDomainsWrapper extends Component {
8789
};
8890

8991
handleDomainExistRequirement = async () => {
90-
this.setState({ loadingDomain: true });
92+
this.setState({ loadingDomain: true, existError: false });
9193
const domain = this.state.domain;
94+
const isValidDomain = domainRegex.test(domain);
95+
if (!isValidDomain) {
96+
this.setState({
97+
loadingDomain: false,
98+
existError: true,
99+
errorMessage: errors.INVALID_FORMAT
100+
});
101+
return;
102+
}
92103
const response = await isDomainAvailable(domain);
93104
if (!response) {
94105
this.setState({

email_mailbox/src/containers/SettingsContainer.js

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {
1616
updateAccount,
1717
updateAlias,
1818
createAlias,
19-
createCustomDomain
19+
createCustomDomain,
20+
updateCustomDomain,
21+
logError
2022
} from './../utils/ipc';
2123
import { appDomain, SectionType } from '../utils/const';
2224
import { defineLastDeviceActivity } from '../utils/TimeUtils';
@@ -53,17 +55,17 @@ const formatDevicesData = devices => {
5355
const checkAddressesAndDomains = async addresses => {
5456
if (!addresses) return;
5557

56-
const aliasinDb = await getAlias({});
57-
const customDomainsinDb = await getCustomDomain({});
58+
const aliasinDb = await getAlias({ accountId: myAccount.id });
59+
const customDomainsinDb = await getCustomDomain({ accountId: myAccount.id });
5860

59-
const aliasesInApi = addresses
61+
let aliasesInApi = addresses
6062
.map(address => {
6163
const domainName = address.domain.name;
6264
return address.aliases.map(alias => {
6365
return {
6466
name: alias.name,
65-
rowid: alias.addressId,
66-
active: alias.status,
67+
rowId: alias.addressId,
68+
active: alias.status ? true : false,
6769
domain: domainName
6870
};
6971
});
@@ -72,77 +74,71 @@ const checkAddressesAndDomains = async addresses => {
7274
const domainsInApi = addresses
7375
.map(address => address.domain)
7476
.filter(domain => domain.name !== appDomain);
77+
const domainsToCreate = [];
78+
const domainsToUpdate = [];
79+
const myApiDomains = new Set();
80+
for (const apiDomain of domainsInApi) {
81+
myApiDomains.add(apiDomain.name);
7582

76-
if (domainsInApi.length !== customDomainsinDb.length) {
77-
if (domainsInApi.length > customDomainsinDb.length) {
78-
await Promise.all(
79-
domainsInApi.map(async domain => {
80-
const customDomainsinDbExist = customDomainsinDb.find(
81-
domainInDb => domain.name === domainInDb.name
82-
);
83-
if (!customDomainsinDbExist) {
84-
const params = {
85-
name: domain.name,
86-
validated: domain.confirmed === 1
87-
};
88-
await createCustomDomain(params);
89-
}
90-
})
91-
);
92-
} else if (customDomainsinDb.length > domainsInApi.length) {
93-
const customDomainsToDelete = customDomainsinDb
94-
.filter(domain => {
95-
const domainsInApiExist = domainsInApi.map(
96-
domainInApi => domainInApi.name === domain.name
97-
);
98-
return !domainsInApiExist;
99-
})
100-
.map(domain => domain.name);
101-
await deleteCustomDomains(customDomainsToDelete.filter(e => e));
83+
const localDomain = customDomainsinDb.find(
84+
domain => apiDomain.name === domain.name
85+
);
86+
if (!localDomain) {
87+
domainsToCreate.push({
88+
...apiDomain,
89+
accountId: myAccount.accountId
90+
});
91+
} else if (localDomain.validated !== apiDomain.confirmed) {
92+
domainsToUpdate.push({
93+
name: localDomain.name,
94+
validated: apiDomain.confirmed,
95+
accountId: myAccount.id
96+
});
10297
}
10398
}
99+
const domainsToDelete = customDomainsinDb
100+
.filter(domain => !myApiDomains.has(domain.name))
101+
.map(domain => domain.name);
104102

105-
if (aliasesInApi.length !== aliasinDb.length) {
106-
if (aliasesInApi.length > aliasinDb.length) {
107-
await Promise.all(
108-
aliasesInApi.map(async aliasInApi => {
109-
const aliasInDbExist = aliasinDb.find(
110-
aliasInDB => aliasInDB.rowid === aliasInApi.rowid
111-
);
112-
if (!aliasInDbExist) {
113-
const alias = {
114-
rowId: aliasInApi.rowid,
115-
name: aliasInApi.name,
116-
domain:
117-
aliasInApi.domain === appDomain ? null : aliasInApi.domain,
118-
active: aliasInApi.active === 1
119-
};
120-
await createAlias(alias);
121-
} else if (
122-
aliasInDbExist &&
123-
// eslint-disable-next-line eqeqeq
124-
aliasInDbExist.active != aliasInApi.active
125-
) {
126-
// make an update
127-
await updateAlias({
128-
rowId: aliasInDbExist.rowId,
129-
active: !aliasInDbExist.active
130-
});
131-
}
132-
})
133-
);
134-
} else if (aliasinDb.length > aliasesInApi.length) {
135-
const aliasToDelete = aliasinDb
136-
.filter(aliasInDB => {
137-
const aliasInApiExist = aliasesInApi.find(
138-
aliasInApi => aliasInApi.rowid === aliasInDB.rowId
139-
);
140-
return !aliasInApiExist;
141-
})
142-
.map(alias => alias.rowId);
143-
await deleteAliases({ rowIds: aliasToDelete.filter(e => e) });
103+
const aliasesToUpdate = [];
104+
const aliasesToDelete = [];
105+
for (const localAlias of aliasinDb) {
106+
if (localAlias.domain !== appDomain && myApiDomains.has(localAlias.domain))
107+
return;
108+
const apiAlias = aliasesInApi.find(
109+
alias => localAlias.rowId === alias.rowId
110+
);
111+
if (!apiAlias) {
112+
aliasesToDelete.push(localAlias.rowId);
113+
} else if (localAlias.active !== apiAlias.active) {
114+
aliasesToUpdate.push({
115+
rowId: localAlias.rowId,
116+
active: apiAlias.active,
117+
accountId: myAccount.id
118+
});
144119
}
120+
aliasesInApi = aliasesInApi.filter(
121+
alias => alias.rowId !== localAlias.rowId
122+
);
145123
}
124+
const aliasesToCreate = aliasesInApi.map(alias => ({
125+
...alias,
126+
domain: alias.domain === appDomain ? null : alias.domain,
127+
accountId: myAccount.id
128+
}));
129+
130+
await Promise.all(
131+
[
132+
domainsToCreate.map(createCustomDomain),
133+
domainsToUpdate.map(updateCustomDomain),
134+
domainsToDelete.length > 0 ? deleteCustomDomains(domainsToDelete) : [],
135+
aliasesToCreate.map(createAlias),
136+
aliasesToUpdate.map(updateAlias),
137+
aliasesToDelete.length > 0
138+
? deleteAliases({ rowIds: aliasesToDelete, accountId: myAccount.id })
139+
: []
140+
].flat()
141+
);
146142
};
147143

148144
const deleteDeviceData = async (onUpdateApp, onClickSection) => {
@@ -190,7 +186,11 @@ const mapDispatchToProps = (dispatch, ownProps) => {
190186
dispatch(reloadAccounts());
191187
}
192188

193-
await checkAddressesAndDomains(addresses); //syncro
189+
try {
190+
await checkAddressesAndDomains(addresses);
191+
} catch (ex) {
192+
logError(ex.stack);
193+
}
194194

195195
const aliases = addresses.reduce((result, domainWithAliases) => {
196196
const myAliases = domainWithAliases.aliases.map(alias => {

email_mailbox/src/lang/de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"title": "Let's set up your domain",
2424
"text": "Enter the custom domain you wish to add.",
2525
"errors": {
26+
"invalidFormat": "This is not a valid domain. Note: do NOT add 'http' or 'www' when entering your domain.",
2627
"usedDomain": "This domain is already in use",
2728
"unknownError": "Server error. Try again later."
2829
}

email_mailbox/src/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"title": "Let's set up your domain",
2424
"text": "Enter the custom domain you wish to add.",
2525
"errors": {
26+
"invalidFormat": "This is not a valid domain. Note: do NOT add 'http' or 'www' when entering your domain.",
2627
"usedDomain": "This domain is already in use",
2728
"unknownError": "Server error. Try again later."
2829
}

email_mailbox/src/lang/es.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"title": "Configuremos tu dominio",
2424
"text": "Ingresa el dominio personal que quisieras agregar.",
2525
"errors": {
26+
"invalidFormat": "Dominio inválido. Nota: NO agregue 'http' o 'www' al escribir su dominio.",
2627
"usedDomain": "Este dominio ya está en uso",
2728
"unknownError": "Error de servidor. Prueba despúes."
2829
}

email_mailbox/src/lang/fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"title": "Let's set up your domain",
2424
"text": "Enter the custom domain you wish to add.",
2525
"errors": {
26+
"invalidFormat": "This is not a valid domain. Note: do NOT add 'http' or 'www' when entering your domain.",
2627
"usedDomain": "This domain is already in use",
2728
"unknownError": "Server error. Try again later."
2829
}

0 commit comments

Comments
 (0)