From 2f2d0ae53873b9e5b3a3f8ce0856dda878b257a2 Mon Sep 17 00:00:00 2001 From: Daniel Petrov Date: Sat, 11 May 2024 15:52:35 +0300 Subject: [PATCH] Feat/fishes total length (#1121) --- __tests__/__snapshots__/db-schema.js.snap | 5 +- server/forms/fishes.js | 7 + ...112145-alter-form-fish-add-total-length.js | 25 +++ ...2759-add-nomenclature-fish-total-length.js | 170 ++++++++++++++++++ 4 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 server/migrations/20240508112145-alter-form-fish-add-total-length.js create mode 100644 server/migrations/20240508112759-add-nomenclature-fish-total-length.js diff --git a/__tests__/__snapshots__/db-schema.js.snap b/__tests__/__snapshots__/db-schema.js.snap index 69ba4e83b..54a5a3297 100644 --- a/__tests__/__snapshots__/db-schema.js.snap +++ b/__tests__/__snapshots__/db-schema.js.snap @@ -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) ); diff --git a/server/forms/fishes.js b/server/forms/fishes.js index 772132d74..40c1bc94e 100644 --- a/server/forms/fishes.js +++ b/server/forms/fishes.js @@ -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' diff --git a/server/migrations/20240508112145-alter-form-fish-add-total-length.js b/server/migrations/20240508112145-alter-form-fish-add-total-length.js new file mode 100644 index 000000000..8ecc0e5ac --- /dev/null +++ b/server/migrations/20240508112145-alter-form-fish-add-total-length.js @@ -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') + } +} diff --git a/server/migrations/20240508112759-add-nomenclature-fish-total-length.js b/server/migrations/20240508112759-add-nomenclature-fish-total-length.js new file mode 100644 index 000000000..ca7f9025e --- /dev/null +++ b/server/migrations/20240508112759-add-nomenclature-fish-total-length.js @@ -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) + } +}