Skip to content
Draft
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
72 changes: 72 additions & 0 deletions packages/api/db/migration/20260317000000_add_farm_note.js
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 packages/api/db/migration/20260317000001_add_farm_notes_read.js
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');
};
19 changes: 19 additions & 0 deletions packages/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@
"@types/express": "^4.17.21",
"@types/jest": "^30.0.0",
"@types/jwk-to-pem": "^2.0.3",
"@types/multer": "^2.1.0",
"@types/node": "^22.5.4",
"@types/ua-parser-js": "^0.7.39",
"@types/uuid": "^10.0.0",
"eslint": "^9.10.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-json": "^4.0.1",
Expand Down
95 changes: 95 additions & 0 deletions packages/api/src/controllers/farmNoteController.js
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() {
Copy link
Collaborator

@kathyavini kathyavini Mar 18, 2026

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.

Copy link
Collaborator

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 🙏

Copy link
Collaborator

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!

Copy link
Collaborator

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!

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;
64 changes: 64 additions & 0 deletions packages/api/src/controllers/farmNotesReadController.js
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;
5 changes: 5 additions & 0 deletions packages/api/src/middleware/acl/hasFarmAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const entitiesGetters = {
product_id: fromProductFarm,
tape_survey_id: fromTapeSurvey,
submission_id: fromTapeSurvey,
farm_note_id: fromFarmNote,
};
import userFarmModel from '../../models/userFarmModel.js';

Expand Down Expand Up @@ -289,6 +290,10 @@ function fromTapeSurvey(submission_id) {
return knex('tape_survey').where({ submission_id }).first();
}

function fromFarmNote(farm_note_id) {
return knex('farm_note').where({ farm_note_id }).first();
}

function sameFarm(object, farm) {
return object.farm_id === farm;
}
Expand Down
Loading
Loading