Skip to content

Commit

Permalink
Feat/fishes total length (#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxvd authored May 11, 2024
1 parent d358056 commit 2f2d0ae
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 1 deletion.
5 changes: 4 additions & 1 deletion __tests__/__snapshots__/db-schema.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,10 @@ CREATE TABLE public.\\"FormFishes\\" (
\\"updatedAt\\" timestamp with time zone,
id integer DEFAULT nextval('public.\\"FormBirds_id_seq\\"'::regclass) NOT NULL,
hash character varying(64),
\\"naturalBarriers\\" boolean
\\"naturalBarriers\\" boolean,
\\"totalLengthEn\\" text,
\\"totalLengthLocal\\" text,
\\"totalLengthLang\\" character varying(3)
);
Expand Down
7 changes: 7 additions & 0 deletions server/forms/fishes.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ exports.fields = assign(exports.fields, {
}
},
naturalBarriers: 'boolean',
totalLength: {
type: 'choice',
relation: {
model: 'nomenclature',
filter: { type: 'fishes_total_length' }
}
},

speciesNotes: {
type: 'text'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'
const { DataTypes } = require('sequelize')

module.exports = {
async up (queryInterface) {
await queryInterface.addColumn('FormFishes', 'totalLengthEn', {
type: DataTypes.TEXT,
allowNull: true
})
await queryInterface.addColumn('FormFishes', 'totalLengthLocal', {
type: DataTypes.TEXT,
allowNull: true
})
await queryInterface.addColumn('FormFishes', 'totalLengthLang', {
type: DataTypes.STRING(3),
allowNull: true
})
},

async down (queryInterface) {
await queryInterface.removeColumn('FormFishes', 'totalLengthEn')
await queryInterface.removeColumn('FormFishes', 'totalLengthLocal')
await queryInterface.removeColumn('FormFishes', 'totalLengthLang')
}
}
170 changes: 170 additions & 0 deletions server/migrations/20240508112759-add-nomenclature-fish-total-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
'use strict'

const Sequelize = require('sequelize')
const capitalizeFirstLetter = require('../utils/capitalizeFirstLetter')

const nomenclatures = {
fishes_total_length: [
{
label: {
bg: '0 - 5 cm',
en: '0 - 5 cm'
}
},
{
label: {
bg: '5 - 10 cm',
en: '5 - 10 cm'
}
},
{
label: {
bg: '10 - 15 cm',
en: '10 - 15 cm'
}
},
{
label: {
bg: '15 - 20 cm',
en: '15 - 20 cm'
}
},
{
label: {
bg: '20 - 25 cm',
en: '20 - 25 cm'
}
},
{
label: {
bg: '25 - 30 cm',
en: '25 - 30 cm'
}
},
{
label: {
bg: '30 - 35 cm',
en: '30 - 35 cm'
}
},
{
label: {
bg: '35 - 40 cm',
en: '35 - 40 cm'
}
},
{
label: {
bg: '40 - 45 cm',
en: '40 - 45 cm'
}
},
{
label: {
bg: '45 - 50 cm',
en: '45 - 50 cm'
}
},
{
label: {
bg: '50 - 55 cm',
en: '50 - 55 cm'
}
},
{
label: {
bg: '55 - 60 cm',
en: '55 - 60 cm'
}
},
{
label: {
bg: '60 - 65 cm',
en: '60 - 65 cm'
}
},
{
label: {
bg: '65 - 70 cm',
en: '65 - 70 cm'
}
},
{
label: {
bg: '70 - 75 cm',
en: '70 - 75 cm'
}
},
{
label: {
bg: '75 - 80 cm',
en: '75 - 80 cm'
}
},
{
label: {
bg: '80 - 85 cm',
en: '80 - 85 cm'
}
},
{
label: {
bg: '85 - 90 cm',
en: '85 - 90 cm'
}
},
{
label: {
bg: '90 - 95 cm',
en: '90 - 95 cm'
}
},
{
label: {
bg: '95 - 100 cm',
en: '95 - 100 cm'
}
},
{
label: {
bg: '> 100 cm',
en: '> 100 cm'
}
}
]
}

const nomenclatureValues = Object.entries(nomenclatures).flatMap(([type, values]) => values.map((value) => ({
createdAt: new Date(),
updatedAt: new Date(),
type,
...Object.fromEntries(Object.keys(value.label).map(lang => [`label${capitalizeFirstLetter(lang)}`, value.label[lang]]))
})))

const deleteExistingValues = (queryInterface) =>
queryInterface.bulkDelete('Nomenclatures', {
type: {
[Sequelize.Op.in]: Object.keys(nomenclatures)
}
})

module.exports = {
async up (queryInterface) {
// we use hard-coded IDs for the nomenclatures in test fixtures and fixtures cannot be applied if the migration is executed
if (queryInterface.sequelize.options.dialect !== 'postgres') {
return
}
await deleteExistingValues(queryInterface)
await Promise.all([
queryInterface.bulkInsert('Nomenclatures', nomenclatureValues)
])
},

async down (queryInterface) {
// we use hard-coded IDs for the nomenclatures in test fixtures and fixtures cannot be applied if the migration is executed
if (queryInterface.sequelize.options.dialect !== 'postgres') {
return
}
await deleteExistingValues(queryInterface)
}
}

0 comments on commit 2f2d0ae

Please sign in to comment.