@@ -17,31 +17,56 @@ import FarmAddonModel from '../models/farmAddonModel.js';
17
17
import { getValidEnsembleOrg } from '../util/ensemble.js' ;
18
18
19
19
const farmAddonController = {
20
- async addFarmAddon ( req , res ) {
21
- const { farm_id } = req . headers ;
22
- const { addon_partner_id, org_uuid } = req . body ;
20
+ addFarmAddon ( ) {
21
+ return async ( req , res ) => {
22
+ const { farm_id } = req . headers ;
23
+ const { addon_partner_id, org_uuid } = req . body ;
23
24
24
- try {
25
- const organisation = await getValidEnsembleOrg ( org_uuid ) ;
25
+ try {
26
+ const organisation = await getValidEnsembleOrg ( org_uuid ) ;
26
27
27
- if ( ! organisation ) {
28
- return res . status ( 404 ) . send ( 'Organisation not found' ) ;
29
- }
28
+ if ( ! organisation ) {
29
+ return res . status ( 404 ) . send ( 'Organisation not found' ) ;
30
+ }
30
31
31
- await FarmAddonModel . upsertFarmAddon ( {
32
- farm_id,
33
- addon_partner_id,
34
- org_uuid,
35
- org_pk : organisation . pk ,
36
- } ) ;
32
+ await FarmAddonModel . upsertFarmAddon ( {
33
+ farm_id,
34
+ addon_partner_id,
35
+ org_uuid,
36
+ org_pk : organisation . pk ,
37
+ } ) ;
37
38
38
- return res . status ( 200 ) . send ( ) ;
39
- } catch ( error ) {
40
- console . error ( error ) ;
41
- return res . status ( error . status || 400 ) . json ( {
42
- error : error . message || error ,
43
- } ) ;
44
- }
39
+ return res . status ( 200 ) . send ( ) ;
40
+ } catch ( error ) {
41
+ console . error ( error ) ;
42
+ return res . status ( error . status || 400 ) . json ( {
43
+ error : error . message || error ,
44
+ } ) ;
45
+ }
46
+ } ;
47
+ } ,
48
+ getFarmAddon ( ) {
49
+ return async ( req , res ) => {
50
+ try {
51
+ const { farm_id } = req . headers ;
52
+ const { addon_partner_id } = req . query ;
53
+ const rows = await FarmAddonModel . query ( )
54
+ . where ( { farm_id, addon_partner_id } )
55
+ . skipUndefined ( ) ;
56
+ if ( ! rows . length ) {
57
+ return res . sendStatus ( 404 ) ;
58
+ }
59
+ const result = rows . map ( ( { addon_partner_id, org_uuid } ) => {
60
+ return { addon_partner_id, org_uuid } ;
61
+ } ) ;
62
+ return res . status ( 200 ) . send ( result ) ;
63
+ } catch ( error ) {
64
+ console . error ( error ) ;
65
+ return res . status ( 500 ) . json ( {
66
+ error,
67
+ } ) ;
68
+ }
69
+ } ;
45
70
} ,
46
71
} ;
47
72
0 commit comments