diff --git a/database/migrations/20240522080439_recentLocationsView.js b/database/migrations/20240522080439_recentLocationsView.js index 385d435..cea08d2 100644 --- a/database/migrations/20240522080439_recentLocationsView.js +++ b/database/migrations/20240522080439_recentLocationsView.js @@ -12,7 +12,7 @@ exports.up = function (knex) { location->>'municipality' AS municipality, "tenant_id", "created_by" AS user_id, - "id" AS "geom", + "id" AS "fishStockingId", "event_time" FROM fish_stockings ORDER BY location->>'cadastral_id', "user_id", "tenant_id", "event_time" DESC; diff --git a/services/fishStockings.service.ts b/services/fishStockings.service.ts index a213497..b366c21 100644 --- a/services/fishStockings.service.ts +++ b/services/fishStockings.service.ts @@ -223,6 +223,7 @@ export type FishStocking< virtual: true, default: () => [], async populate(ctx: Context, _values: any, fishStockings: FishStocking[]) { + if (!ctx) return; const fishBatches: FishBatch[] = await ctx.call('fishBatches.find', { query: { fishStocking: { @@ -364,6 +365,7 @@ export type FishStocking< virtual: true, default: () => [], async populate(ctx: Context, _values: any, fishStockings: FishStocking[]) { + if (!ctx) return; const fishBatches: FishBatch[] = await ctx.call('fishBatches.find', { query: { fishStocking: { @@ -389,6 +391,7 @@ export type FishStocking< //TODO: mandatory flag could be part of location object virtual: true, get: async ({ entity, ctx }: FieldHookCallback) => { + if (!ctx) return; const area = entity.location.area; if (area && area > 50) { return true; @@ -423,18 +426,14 @@ export type FishStocking< } return { ...query, - $raw: { - condition: `("location"::jsonb->'municipality'->'id')::int in (${ctx.meta.authUser.municipalities?.toString()})`, - }, + $raw: `("location"::jsonb->'municipality'->'id')::int in (${ctx.meta.authUser.municipalities?.toString()})`, }; } // sesijoj imone if (ctx.meta.profile && ctx.meta?.user) { return { ...query, - $raw: { - condition: `(tenant_id = ${ctx.meta.profile} OR stocking_customer_id = ${ctx.meta.profile})`, - }, + $raw: `(tenant_id = ${ctx.meta.profile} OR stocking_customer_id = ${ctx.meta.profile})`, }; } @@ -452,7 +451,7 @@ export type FishStocking< ...COMMON_SCOPES, }, defaultScopes: [...COMMON_DEFAULT_SCOPES, 'profile'], - defaultPopulates: ['batches', 'status', 'mandatory'], + defaultPopulates: ['batches', 'status'], }, hooks: { before: { @@ -1029,8 +1028,7 @@ export default class FishStockingsService extends moleculer.Service { auth: RestrictionType.USER, }) async getRecentLocations(ctx: Context) { - const recentLocations = await ctx.call('recentLocations.list'); - return recentLocations; + return await ctx.call('recentLocations.list'); } @Action() @@ -1553,7 +1551,7 @@ export default class FishStockingsService extends moleculer.Service { ]; for (const item of data) { - const fishStocking = await this.createEntity(null, item, { permissive: true }); + const fishStocking = await this.createEntity(null, item); if (fishStocking?.id) { const batches = item.batches.map((batch: FishBatch) => ({ ...batch, diff --git a/services/locations.service.ts b/services/locations.service.ts index 92abb0a..797d28c 100644 --- a/services/locations.service.ts +++ b/services/locations.service.ts @@ -75,16 +75,14 @@ export default class LocationsService extends moleculer.Service { 'ISOLATED_WATER_BODY', ], }, - ...params.query, + ...(params.query || {}), }; searchParams.set('query', JSON.stringify(query)); const queryString = searchParams.toString(); const url = `${targetUrl}?${queryString}`; try { - const response = await fetch(url); - const data = await response.json(); - + const data = await fetch(url).then((r) => r.json()); const municipalities = await this.actions.getMunicipalities(null, { parentCtx: ctx }); const rows = data?.rows?.map((item: any) => ({ name: item.name, diff --git a/services/recentLocations.service.ts b/services/recentLocations.service.ts index 5776166..ff2aa3f 100644 --- a/services/recentLocations.service.ts +++ b/services/recentLocations.service.ts @@ -5,7 +5,6 @@ import { Method, Service } from 'moleculer-decorators'; import DbConnection from '../mixins/database.mixin'; import { RestrictionType } from '../types'; import { UserAuthMeta } from './api.service'; -import { FishStocking } from './fishStockings.service'; const mapItem = (data: RecentLocation) => { const { cadastralId, ...rest } = data; @@ -19,6 +18,7 @@ export interface RecentLocation { id: number; name: string; }; + fishStockingId: number; geom: any; } @@ -46,18 +46,28 @@ export interface RecentLocation { name: 'string', }, }, + fishStockingId: { + type: 'number', + columnType: 'integer', + columnName: 'fishStockingId', + required: true, + immutable: true, + hidden: 'byDefault', + }, geom: { type: 'any', - populate: async (ctx: Context, _values: any, entities: RecentLocation[]) => { - const fishStockingIds = entities.map((entity) => entity.geom); - const fishStockings: FishStocking[] = await ctx.call('fishStockings.find', { - query: { - id: { $in: fishStockingIds }, - }, - scope: false, - populate: ['geom'], - }); - return entities.map((entity) => fishStockings.find((f) => f.id === entity.geom)?.geom); + raw: true, + virtual: true, + async populate(ctx: any, _values: any, recentLocations: RecentLocation[]) { + return Promise.all( + recentLocations.map(async (recentLocation) => { + return ctx.call('fishStockings.getGeometryJson', { + field: 'geom', + asField: 'geom', + id: recentLocation.fishStockingId, + }); + }), + ); }, }, tenant: {