Skip to content
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

57 galimybe filtruoti uetk statistika #58

Merged
merged 12 commits into from
Apr 11, 2024
Merged
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
57 changes: 57 additions & 0 deletions database/migrations/20240404063150_completedFishstockingsView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
exports.up = function (knex) {
return knex.schema.createViewOrReplace('fishStockingsCompleted', function (view) {
view.as(
knex.raw(`
WITH fb AS (
SELECT
fb.fish_stocking_id,
json_agg(
json_build_object(
'fish_type',
json_build_object('id', ft.id, 'label', ft.label),
'fish_age',
json_build_object('id', fa.id, 'label', fa.label),
'count',
fb.review_amount,
'weight',
fb.review_weight
)
) AS fish_batches
FROM
public.fish_batches fb
LEFT JOIN public.fish_types ft ON ft.id = fb.fish_type_id
LEFT JOIN public.fish_ages fa ON fa.id = fb.fish_age_id
GROUP BY
fb.fish_stocking_id
)
SELECT
s.id,
s.event_time,
s.review_time,
s.geom,
s.location::json,
fb.fish_batches
FROM
public.fish_stockings s
LEFT JOIN fb ON fb.fish_stocking_id = s.id
WHERE
EXISTS (
SELECT
1
FROM
public.fish_batches fb
WHERE
fb.fish_stocking_id = s.id
AND fb.review_amount IS NOT NULL
AND fb.deleted_at IS NULL
)
AND s.review_time IS NOT NULL
AND s.deleted_at IS NULL
`),
);
});
};

exports.down = function (knex) {
return knex.schema.dropViewIfExists('fishStockingsCompleted');
};
73 changes: 39 additions & 34 deletions services/fishBatches.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import moleculer, { Context } from 'moleculer';
import {Action, Method, Service} from 'moleculer-decorators';
import { Action, Method, Service } from 'moleculer-decorators';

import { filter, map } from 'lodash';
import DbConnection from '../mixins/database.mixin';
Expand All @@ -13,11 +13,10 @@ import {
CommonPopulates,
Table,
} from '../types';
import { UserAuthMeta } from './api.service';
import { FishAge } from './fishAges.service';
import { FishStocking } from './fishStockings.service';
import { FishType } from './fishTypes.service';
import {UserAuthMeta} from "./api.service";


interface Fields extends CommonFields {
id: number;
Expand Down Expand Up @@ -95,7 +94,6 @@ export type FishBatch<
},
})
export default class FishBatchesService extends moleculer.Service {

@Action({
params: {
batches: {
Expand All @@ -107,8 +105,8 @@ export default class FishBatchesService extends moleculer.Service {
fishAge: 'number|integer|positive',
amount: 'number|integer|positive',
weight: 'number|positive|optional',
}
}
},
},
},
fishStocking: 'number|integer|positive',
},
Expand All @@ -121,13 +119,12 @@ export default class FishBatchesService extends moleculer.Service {
) {
if (ctx.params.batches) {
const batches = ctx.params.batches?.map((batch) => ({
fishType: batch.fishType,
fishAge: batch.fishAge,
amount: batch.amount,
weight: batch.weight,
fishStocking: ctx.params.fishStocking,
})
)
fishType: batch.fishType,
fishAge: batch.fishAge,
amount: batch.amount,
weight: batch.weight,
fishStocking: ctx.params.fishStocking,
}));
await ctx.call('fishBatches.createMany', batches);
}
}
Expand All @@ -147,20 +144,22 @@ export default class FishBatchesService extends moleculer.Service {
weight: 'number|optional',
reviewAmount: 'number|integer|positive|optional',
reviewWeight: 'number|optional',
}
}
},
},
},
fishStocking: 'number|integer|positive',
},
})
//for admin
async updateBatches(
ctx: Context<{
batches: FishBatch[];
fishStocking: number;
}, UserAuthMeta>,
ctx: Context<
{
batches: FishBatch[];
fishStocking: number;
},
UserAuthMeta
>,
) {

await this.deleteExistingBatches(ctx, ctx.params.fishStocking, ctx.params.batches);
await this.createOrUpdateBatches(ctx, ctx.params.fishStocking, ctx.params.batches);
return await this.findEntities(ctx, {
Expand All @@ -182,20 +181,22 @@ export default class FishBatchesService extends moleculer.Service {
fishType: 'number|integer|positive|optional',
fishAge: 'number|integer|positive|optional',
amount: 'number|integer|positive',
weight: 'number|optional'
}
}
weight: 'number|optional',
},
},
},
fishStocking: 'number|integer|positive',
},
})
async updateRegisteredBatches(
ctx: Context<{
ctx: Context<
{
batches: FishBatch[];
fishStocking: number;
}, UserAuthMeta>,
},
UserAuthMeta
>,
) {

await this.deleteExistingBatches(ctx, ctx.params.fishStocking, ctx.params.batches);
await this.createOrUpdateBatches(ctx, ctx.params.fishStocking, ctx.params.batches);

Expand All @@ -216,18 +217,21 @@ export default class FishBatchesService extends moleculer.Service {
properties: {
id: 'number|integer|positive',
reviewAmount: 'number|integer|positive',
reviewWeight: 'number|optional'
}
}
reviewWeight: 'number|optional',
},
},
},
fishStocking: 'number|integer|positive',
},
})
async reviewBatches(
ctx: Context<{
ctx: Context<
{
batches: FishBatch[];
fishStocking: number;
}, UserAuthMeta>,
},
UserAuthMeta
>,
) {
await this.deleteExistingBatches(ctx, ctx.params.fishStocking, ctx.params.batches);
await this.createOrUpdateBatches(ctx, ctx.params.fishStocking, ctx.params.batches);
Expand All @@ -249,16 +253,17 @@ export default class FishBatchesService extends moleculer.Service {
query: { fishStocking: fishStockingId },
});
const deleteBatches = filter(
existingBatches,
(existingBatch: FishBatch) => batches?.find((batch) => batch.id && existingBatch.id == batch.id)
existingBatches,
(existingBatch: FishBatch) =>
!batches?.find((batch) => batch.id && existingBatch.id == batch.id),
);
const promises = map(deleteBatches, (batch: FishBatch) => this.removeEntity(ctx, batch));
await Promise.all(promises);
}

@Method
async createOrUpdateBatches(ctx: Context, fishStocking: number, batches: any[]) {
const promises = batches?.map( (batch: FishBatch) => {
const promises = batches?.map((batch: FishBatch) => {
if (batch.id) {
return this.updateEntity(ctx, batch);
}
Expand Down
Loading
Loading