Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DovMa committed Jun 18, 2024
1 parent 836b09c commit 6c19bef
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion database/migrations/20240522080439_recentLocationsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 8 additions & 10 deletions services/fishStockings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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;
Expand Down Expand Up @@ -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})`,
};
}

Expand All @@ -452,7 +451,7 @@ export type FishStocking<
...COMMON_SCOPES,
},
defaultScopes: [...COMMON_DEFAULT_SCOPES, 'profile'],
defaultPopulates: ['batches', 'status', 'mandatory'],
defaultPopulates: ['batches', 'status'],
},
hooks: {
before: {
Expand Down Expand Up @@ -1029,8 +1028,7 @@ export default class FishStockingsService extends moleculer.Service {
auth: RestrictionType.USER,
})
async getRecentLocations(ctx: Context<any, UserAuthMeta>) {
const recentLocations = await ctx.call('recentLocations.list');
return recentLocations;
return await ctx.call('recentLocations.list');
}

@Action()
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions services/locations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 21 additions & 11 deletions services/recentLocations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +18,7 @@ export interface RecentLocation {
id: number;
name: string;
};
fishStockingId: number;
geom: any;
}

Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 6c19bef

Please sign in to comment.