-
Notifications
You must be signed in to change notification settings - Fork 93
LF-4995: Create post endpoint to add market directory info #3914
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
Merged
kathyavini
merged 26 commits into
integration
from
LF-4995/Create_POST_endpoint_to_add_Market_directory_info
Nov 4, 2025
Merged
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e26e89a
LF-4995 Migration to add market_directory_info permissions
SayakaOno 2c1e1b6
LF-4995 Add POST API skeleton for market directory info
SayakaOno f4cd6fb
LF-4995 Add validation functions
SayakaOno 093be65
LF-4995 Add email validation function
SayakaOno 05f7dd2
LF-4995 Add shared validation functions
SayakaOno da19ee7
LF-4995 Move util functions
SayakaOno 9e7783e
LF-4995 Update LiteFarmRequest interface
SayakaOno a103b03
LF-4995 Add getAddressComponents
SayakaOno 4d977a5
LF-4995 Add isValidAddress function
SayakaOno 183913b
LF-4995 Add isValidAddress function
SayakaOno f68a0b8
LF-4995 Update marketDirectoryInfo validation middleware
SayakaOno ad5ad3b
LF-4995 Update addMarketDirectoryInfo method
SayakaOno f5001c5
LF-4995 Update checkAndTransformMarketDirectoryInfo
SayakaOno f02ae7f
LF-4995 Update mock factories and tableCleanup
SayakaOno 47773c7
LF-4995 Fix checkAndTransformMarketDirectoryInfo
SayakaOno 627fa6d
LF-4995 Update marketDirectoryInfo properties maxLength in model
SayakaOno be5ca92
LF-4995 Fix typo in mock factories
SayakaOno 8e62955
LF-4995 Add createUserFarmIds, HeadersParams for tests
SayakaOno f46d09d
LF-4995 Add tests for 'Post Market Directory Info'
SayakaOno 8737b50
LF-4995 Add permission for get:market_directory_info
SayakaOno 3ded73e
LF-4995 Update social validation
SayakaOno c720489
LF-4995 Add tests for validateSocialAndExtractUsername
SayakaOno fb4edc8
LF-4995 Update tsconfig to exclude test files
SayakaOno 8990ace
LF-4995 Move test file
SayakaOno fb9be43
Revert "LF-4995 Update tsconfig to exclude test files"
SayakaOno 74e1b14
LF-4995 Improve tests
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
70 changes: 70 additions & 0 deletions
70
packages/api/db/migration/20251024195921_add_market_directory_info_permissions.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,70 @@ | ||
| /* | ||
| * Copyright 2025 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('permissions').insert([ | ||
| { | ||
| permission_id: 179, | ||
| name: 'add:market_directory_info', | ||
| description: 'add market_directory_info', | ||
| }, | ||
| { | ||
| permission_id: 180, | ||
| name: 'edit:market_directory_info', | ||
| description: 'edit market_directory_info', | ||
| }, | ||
| { | ||
| permission_id: 181, | ||
| name: 'delete:market_directory_info', | ||
| description: 'delete market_directory_info', | ||
| }, | ||
| { | ||
| permission_id: 182, | ||
| name: 'get:market_directory_info', | ||
| description: 'get market_directory_info', | ||
| }, | ||
| ]); | ||
| await knex('rolePermissions').insert([ | ||
| { role_id: 1, permission_id: 179 }, | ||
| { role_id: 2, permission_id: 179 }, | ||
| { role_id: 5, permission_id: 179 }, | ||
| { role_id: 1, permission_id: 180 }, | ||
| { role_id: 2, permission_id: 180 }, | ||
| { role_id: 5, permission_id: 180 }, | ||
| { role_id: 1, permission_id: 181 }, | ||
| { role_id: 2, permission_id: 181 }, | ||
| { role_id: 5, permission_id: 181 }, | ||
| { role_id: 1, permission_id: 182 }, | ||
| { role_id: 2, permission_id: 182 }, | ||
| { role_id: 5, permission_id: 182 }, | ||
| ]); | ||
| }; | ||
|
|
||
| /** | ||
| * @param { import("knex").Knex } knex | ||
| * @returns { Promise<void> } | ||
| */ | ||
| export const down = function (knex) { | ||
| const permissions = [179, 180, 181, 182]; | ||
| return Promise.all([ | ||
| knex('rolePermissions').whereIn('permission_id', permissions).del(), | ||
| knex('permissions').whereIn('permission_id', permissions).del(), | ||
| ]); | ||
| }; |
50 changes: 50 additions & 0 deletions
50
packages/api/src/controllers/marketDirectoryInfoController.ts
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,50 @@ | ||
| /* | ||
| * Copyright 2025 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 { Response } from 'express'; | ||
| import baseController from './baseController.js'; | ||
| import MarketDirectoryInfoModel from '../models/marketDirectoryInfoModel.js'; | ||
| import { MarketDirectoryInfoReqBody } from '../middleware/validation/checkMarketDirectoryInfo.js'; | ||
| import { HttpError, LiteFarmRequest } from '../types.js'; | ||
|
|
||
| const marketDirectoryInfoController = { | ||
| addMarketDirectoryInfo() { | ||
| return async ( | ||
| req: LiteFarmRequest<unknown, unknown, unknown, MarketDirectoryInfoReqBody>, | ||
| res: Response, | ||
| ) => { | ||
| const { farm_id } = req.headers; | ||
|
|
||
| try { | ||
| const result = await baseController.post( | ||
| MarketDirectoryInfoModel, | ||
| { ...req.body, farm_id }, | ||
| req, | ||
| ); | ||
|
|
||
| return res.status(201).send(result); | ||
| } catch (error: unknown) { | ||
| console.error(error); | ||
| const err = error as HttpError; | ||
| const status = err.status || err.code || 500; | ||
| return res.status(status).json({ | ||
| error: err.message || err, | ||
| }); | ||
| } | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default marketDirectoryInfoController; |
93 changes: 93 additions & 0 deletions
93
packages/api/src/middleware/validation/checkMarketDirectoryInfo.ts
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,93 @@ | ||
| /* | ||
| * Copyright 2025 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 { Response, NextFunction } from 'express'; | ||
| import { LiteFarmRequest } from '../../types.js'; | ||
| import { isValidAddress, isValidEmail } from '../../util/validation.js'; | ||
| import { isValidUrl } from '../../util/url.js'; | ||
| import { SOCIALS, validateAndExtractUsernameOrUrl } from '../../util/socials.js'; | ||
| import MarketDirectoryInfoModel from '../../models/marketDirectoryInfoModel.js'; | ||
|
|
||
| export interface MarketDirectoryInfoReqBody { | ||
| farm_name?: string; | ||
| logo?: string; | ||
| about?: string; | ||
| contact_first_name?: string; | ||
| contact_last_name?: string; | ||
| contact_email?: string; | ||
| email?: string; | ||
| country_code?: number; | ||
| phone_number?: string; | ||
| address?: string; | ||
| website?: string; | ||
| instagram?: string; | ||
| facebook?: string; | ||
| x?: string; | ||
| } | ||
|
|
||
| export function checkAndTransformMarketDirectoryInfo() { | ||
| return async ( | ||
| req: LiteFarmRequest<unknown, unknown, unknown, MarketDirectoryInfoReqBody>, | ||
| res: Response, | ||
| next: NextFunction, | ||
| ) => { | ||
| if (req.method === 'POST') { | ||
| // @ts-expect-error: TS doesn't see query() through softDelete HOC; safe at runtime | ||
| const record = await MarketDirectoryInfoModel.query() | ||
| .where({ farm_id: req.headers.farm_id }) | ||
| .first(); | ||
|
|
||
| if (record) { | ||
| return res.status(409).send('Market directory info for this farm already exists'); | ||
| } | ||
| } | ||
|
|
||
| const { address, website } = req.body; | ||
|
|
||
| for (const emailProperty of ['contact_email', 'email'] as const) { | ||
| if (req.body[emailProperty] && !isValidEmail(req.body[emailProperty])) { | ||
| return res.status(400).send(`Invalid ${emailProperty}`); | ||
| } | ||
| } | ||
|
|
||
| if (address && !(await isValidAddress(address))) { | ||
| return res.status(400).send('Invalid address'); | ||
| } | ||
|
|
||
| if (website && !(await isValidUrl(website)) /* TODO: LF-5011 */) { | ||
| return res.status(400).send('Invalid website'); | ||
| } | ||
|
|
||
| const invalidSocials = ( | ||
| await Promise.all( | ||
| SOCIALS.filter((social) => req.body[social]?.trim()).map(async (social) => { | ||
| const formattedSocial = await validateAndExtractUsernameOrUrl(social, req.body[social]!); | ||
|
|
||
| if (formattedSocial) { | ||
| req.body[social] = formattedSocial; | ||
| return null; | ||
| } | ||
| return social; | ||
| }), | ||
| ) | ||
| ).filter((social) => social !== null); | ||
|
|
||
| if (invalidSocials.length) { | ||
| return res.status(400).send(`Invalid ${invalidSocials.join(', ')}`); | ||
| } | ||
|
|
||
| next(); | ||
| }; | ||
| } |
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,30 @@ | ||
| /* | ||
| * Copyright 2025 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 express from 'express'; | ||
| import checkScope from '../middleware/acl/checkScope.js'; | ||
| import { checkAndTransformMarketDirectoryInfo } from '../middleware/validation/checkMarketDirectoryInfo.js'; | ||
| import MarketDirectoryInfoController from '../controllers/marketDirectoryInfoController.js'; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.post( | ||
| '/', | ||
| checkScope(['add:market_directory_info']), | ||
| checkAndTransformMarketDirectoryInfo(), | ||
| MarketDirectoryInfoController.addMarketDirectoryInfo(), | ||
| ); | ||
|
|
||
| export default router; |
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
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,31 @@ | ||
| /* | ||
| * Copyright 2025 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 { Client } from '@googlemaps/google-maps-services-js'; | ||
| const googleClient = new Client({}); | ||
|
|
||
| export async function getAddressComponents(address: string) { | ||
| try { | ||
| const response = await googleClient.geocode({ | ||
| params: { | ||
| address, | ||
| key: process.env.GOOGLE_API_KEY!, | ||
| }, | ||
| }); | ||
| return response.data.results[0]?.address_components; | ||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
| } |
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.
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.
maxLengthset based on typical browser URL limits: https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers