Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions create-a-container/models/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ module.exports = (sequelize, DataTypes) => {
as: 'externalDomains'
});
}

/**
* Returns all external domains sorted so that domains whose default site is
* this site appear first (in id order), followed by all other domains (also
* in id order).
* @returns {Promise<Array>} Sorted array of ExternalDomain instances
*/
async getSortedExternalDomains() {
const { ExternalDomain } = sequelize.models;
const all = await ExternalDomain.findAll({ order: [['id', 'ASC']] });
const defaults = [];
const others = [];
for (const domain of all) {
if (domain.siteId === this.id) {
defaults.push(domain);
} else {
others.push(domain);
}
}
return [...defaults, ...others];
}
}
Site.init({
name: DataTypes.STRING,
Expand Down
9 changes: 4 additions & 5 deletions create-a-container/routers/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ router.get('/new', requireAuth, async (req, res) => {
return res.redirect('/sites');
}

// Get all external domains (global)
const externalDomains = await ExternalDomain.findAll({
order: [['name', 'ASC']]
});
// Get all external domains: default domains for this site first (by id), then others (by id)
const externalDomains = await site.getSortedExternalDomains();

if (isApi) {
return res.json({
Expand Down Expand Up @@ -243,7 +241,8 @@ router.get('/:id/edit', requireAuth, async (req, res) => {
return res.redirect(`/sites/${siteId}/containers`);
}

const externalDomains = await ExternalDomain.findAll({ order: [['name', 'ASC']] });
// Get all external domains: default domains for this site first (by id), then others (by id)
const externalDomains = await site.getSortedExternalDomains();

return res.render('containers/form', {
site,
Expand Down
Loading