-
Notifications
You must be signed in to change notification settings - Fork 101
LF-5211: Farm notes data model and API #4091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
litefarm-pr-bot
wants to merge
5
commits into
integration
Choose a base branch
from
LF-5211-farm-notes-data-model-and-api
base: integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,102
−1
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3247702
LF-5211: Add farm notes data model and API
litefarm-pr-bot f3c3a31
LF-5211 Combine two migration files
SayakaOno 6f997ff
LF-5211 Adjust table schema
SayakaOno 63fc263
LF-5211 Extract farm_note_id validation
SayakaOno 343aa8b
LF-5211 Create checkFarmNoteBody middleware
SayakaOno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2026 LiteFarm.org | ||
| * This file is part of LiteFarm. | ||
| * | ||
| * LiteFarm is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * LiteFarm is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| /** | ||
| * @param { import("knex").Knex } knex | ||
| * @returns { Promise<void> } | ||
| */ | ||
| export const up = async function (knex) { | ||
| await knex.schema.createTable('farm_note', (table) => { | ||
| table.uuid('farm_note_id').primary().defaultTo(knex.raw('uuid_generate_v4()')); | ||
| table.uuid('farm_id').notNullable().references('farm_id').inTable('farm'); | ||
| table.string('user_id').notNullable().references('user_id').inTable('users'); | ||
| table.text('note').notNullable(); | ||
| table.boolean('is_private').notNullable().defaultTo(false); | ||
| table.text('image_url'); | ||
| table.string('created_by_user_id').references('user_id').inTable('users'); | ||
| table.string('updated_by_user_id').references('user_id').inTable('users'); | ||
| table.dateTime('created_at').notNullable().defaultTo(knex.fn.now()); | ||
| table.dateTime('updated_at').notNullable().defaultTo(knex.fn.now()); | ||
| table.boolean('deleted').notNullable().defaultTo(false); | ||
| }); | ||
|
|
||
| await knex('permissions').insert([ | ||
| { permission_id: 188, name: 'get:farm_notes', description: 'get farm_notes' }, | ||
| { permission_id: 189, name: 'add:farm_notes', description: 'add farm_notes' }, | ||
| { permission_id: 190, name: 'edit:farm_notes', description: 'edit farm_notes' }, | ||
| { permission_id: 191, name: 'delete:farm_notes', description: 'delete farm_notes' }, | ||
| ]); | ||
|
|
||
| await knex('rolePermissions').insert([ | ||
| { role_id: 1, permission_id: 188 }, | ||
| { role_id: 2, permission_id: 188 }, | ||
| { role_id: 3, permission_id: 188 }, | ||
| { role_id: 5, permission_id: 188 }, | ||
| { role_id: 1, permission_id: 189 }, | ||
| { role_id: 2, permission_id: 189 }, | ||
| { role_id: 3, permission_id: 189 }, | ||
| { role_id: 5, permission_id: 189 }, | ||
| { role_id: 1, permission_id: 190 }, | ||
| { role_id: 2, permission_id: 190 }, | ||
| { role_id: 3, permission_id: 190 }, | ||
| { role_id: 5, permission_id: 190 }, | ||
| { role_id: 1, permission_id: 191 }, | ||
| { role_id: 2, permission_id: 191 }, | ||
| { role_id: 3, permission_id: 191 }, | ||
| { role_id: 5, permission_id: 191 }, | ||
| ]); | ||
| }; | ||
|
|
||
| /** | ||
| * @param { import("knex").Knex } knex | ||
| * @returns { Promise<void> } | ||
| */ | ||
| export const down = async function (knex) { | ||
| await knex.schema.dropTable('farm_note'); | ||
|
|
||
| const permissions = [188, 189, 190, 191]; | ||
| await knex('rolePermissions').whereIn('permission_id', permissions).del(); | ||
| await knex('permissions').whereIn('permission_id', permissions).del(); | ||
| }; |
35 changes: 35 additions & 0 deletions
35
packages/api/db/migration/20260317000001_add_farm_notes_read.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright 2026 LiteFarm.org | ||
| * This file is part of LiteFarm. | ||
| * | ||
| * LiteFarm is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * LiteFarm is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| /** | ||
| * @param { import("knex").Knex } knex | ||
| * @returns { Promise<void> } | ||
| */ | ||
| export const up = async function (knex) { | ||
| await knex.schema.createTable('farm_notes_read', (table) => { | ||
| table.string('user_id').notNullable().references('user_id').inTable('users'); | ||
| table.uuid('farm_id').notNullable().references('farm_id').inTable('farm'); | ||
| table.dateTime('last_read_at').notNullable(); | ||
| table.primary(['user_id', 'farm_id']); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * @param { import("knex").Knex } knex | ||
| * @returns { Promise<void> } | ||
| */ | ||
| export const down = async function (knex) { | ||
| await knex.schema.dropTable('farm_notes_read'); | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright 2026 LiteFarm.org | ||
| * This file is part of LiteFarm. | ||
| * | ||
| * LiteFarm is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * LiteFarm is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import FarmNoteModel from '../models/farmNoteModel.js'; | ||
|
|
||
| const farmNoteController = { | ||
| getFarmNotes() { | ||
| return async (req, res) => { | ||
| try { | ||
| const { farm_id } = req.headers; | ||
| const { user_id } = req.auth; | ||
|
|
||
| const notes = await FarmNoteModel.query() | ||
| .whereNotDeleted() | ||
| .where('farm_id', farm_id) | ||
| .where(function () { | ||
| this.where('is_private', false).orWhere('user_id', user_id); | ||
| }) | ||
| .orderBy('created_at', 'desc'); | ||
|
|
||
| return res.status(200).json(notes); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return res.status(500).json({ error }); | ||
| } | ||
| }; | ||
| }, | ||
|
|
||
| createFarmNote() { | ||
| return async (req, res) => { | ||
| try { | ||
| const { farm_id } = req.headers; | ||
| const { user_id } = req.auth; | ||
|
|
||
| const note = await FarmNoteModel.query() | ||
| .context({ user_id }) | ||
| .insert({ farm_id, user_id, ...res.locals.farmNoteData }) | ||
| .returning('*'); | ||
|
|
||
| return res.status(201).json(note); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return res.status(500).json({ error }); | ||
| } | ||
| }; | ||
| }, | ||
|
|
||
| editFarmNote() { | ||
| return async (req, res) => { | ||
| try { | ||
| const { farm_note_id } = req.params; | ||
| const { user_id } = req.auth; | ||
|
|
||
| const updated = await FarmNoteModel.query() | ||
| .context({ user_id }) | ||
| .patchAndFetchById(farm_note_id, res.locals.farmNoteData); | ||
|
|
||
| return res.status(200).json(updated); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return res.status(500).json({ error }); | ||
| } | ||
| }; | ||
| }, | ||
|
|
||
| deleteFarmNote() { | ||
| return async (req, res) => { | ||
| try { | ||
| const { farm_note_id } = req.params; | ||
| const { user_id } = req.auth; | ||
|
|
||
| await FarmNoteModel.query().context({ user_id }).deleteById(farm_note_id); | ||
|
|
||
| return res.status(200).json({ message: 'Note deleted' }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return res.status(500).json({ error }); | ||
| } | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default farmNoteController; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2026 LiteFarm.org | ||
| * This file is part of LiteFarm. | ||
| * | ||
| * LiteFarm is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * LiteFarm is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import FarmNotesReadModel from '../models/farmNotesReadModel.js'; | ||
|
|
||
| const farmNotesReadController = { | ||
| getFarmNotesRead() { | ||
| return async (req, res) => { | ||
| try { | ||
| const { user_id } = req.auth; | ||
| const { farm_id } = req.headers; | ||
|
|
||
| const row = await FarmNotesReadModel.query().where({ user_id, farm_id }).first(); | ||
|
|
||
| return res.status(200).json({ last_read_at: row ? row.last_read_at : null }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return res.status(500).json({ error }); | ||
| } | ||
| }; | ||
| }, | ||
|
|
||
| markFarmNotesRead() { | ||
| return async (req, res) => { | ||
| try { | ||
| const { user_id } = req.auth; | ||
| const { farm_id } = req.headers; | ||
| const last_read_at = new Date().toISOString(); | ||
|
|
||
| const existing = await FarmNotesReadModel.query().where({ user_id, farm_id }).first(); | ||
|
|
||
| if (existing) { | ||
| await FarmNotesReadModel.query() | ||
| .context({ user_id }) | ||
| .patch({ last_read_at }) | ||
| .where({ user_id, farm_id }); | ||
| } else { | ||
| await FarmNotesReadModel.query() | ||
| .context({ user_id }) | ||
| .insert({ user_id, farm_id, last_read_at }); | ||
| } | ||
|
|
||
| return res.status(204).send(); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return res.status(500).json({ error }); | ||
| } | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default farmNotesReadController; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SayakaOno I saw this in the RTK Query endpoint too, but I have no idea why the file / image is excluded from the PATCH; I don't think that was part of the plan (or maybe I missed it!)
I'm double-checking with Loïc here, but I'm pretty sure anything that can be set in POST (including image) should be PATCH-able.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For image removal on the PATCH, I am planning to send
data.image_url = null. Let me know if that works for you 🙏There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should work, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just FYI, I've updated the patch request body and pushed!