Skip to content

Commit

Permalink
Merge pull request #47 from AplinkosMinisterija/ivesti-zuvu-rikiavimo…
Browse files Browse the repository at this point in the history
…-sekas

Ivesti zuvu rikiavimo sekas
  • Loading branch information
DovMa authored Jan 23, 2024
2 parents 8c83266 + 7d2a8c2 commit e6d65f2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 11 deletions.
19 changes: 19 additions & 0 deletions database/migrations/20240109154339_fishTypePriority.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
await knex.schema.alterTable('fishTypes', (table) => {
table.integer('priority').defaultTo(0);
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
await knex.schema.alterTable('fishTypes', (table) => {
table.dropColumn('priority');
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dependencies": {
"@moleculer/database": "github:ambrazasp/moleculerjs-database",
"@moleculer/lab": "^0.6.4",
"@r2d2bzh/moleculer-cron": "^0.1.4",
"biip-auth-nodejs": "github:dadpatch/biip-auth-nodejs",
"date-fns": "^2.29.3",
"dotenv": "^16.0.3",
Expand Down
59 changes: 49 additions & 10 deletions services/fishTypes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
Table,
} from '../types';

const Cron = require('@r2d2bzh/moleculer-cron');

interface Fields extends CommonFields {
id: number;
label: string;
Expand All @@ -35,6 +37,7 @@ export type FishType<
createMany: false,
},
}),
Cron,
],
settings: {
fields: {
Expand All @@ -44,23 +47,41 @@ export type FishType<
secure: true,
},
label: 'string|required',
priority: 'number',
...COMMON_FIELDS,
},
scopes: {
...COMMON_SCOPES,
},
actions: {
remove: {
types: [RestrictionType.ADMIN],
},
create: {
types: [RestrictionType.ADMIN],
},
update: {
types: [RestrictionType.ADMIN],
defaultScopes: [...COMMON_DEFAULT_SCOPES],
},
actions: {
remove: {
auth: RestrictionType.ADMIN,
},
create: {
auth: RestrictionType.ADMIN,
},
update: {
auth: RestrictionType.ADMIN,
},
},
crons: [
{
name: 'updatePriority',
cronTime: '0 0 * * 0',
async onTick() {
return await this.call('fishTypes.updatePriority');
},
timeZone: 'Europe/Vilnius',
},
],
hooks: {
before: {
list: ['sortItems'],
find: ['sortItems'],
all: ['sortItems'],
},
defaultScopes: [...COMMON_DEFAULT_SCOPES],
},
})
export default class FishTypesService extends moleculer.Service {
Expand Down Expand Up @@ -110,4 +131,22 @@ export default class FishTypesService extends moleculer.Service {
{ label: 'margieji plačiakakčiai' },
]);
}

@Method
async sortItems(ctx: Context<any>) {
ctx.params.sort = ctx.params.sort || '-priority,label';
}

@Action()
async updatePriority(ctx: Context) {
const fishTypes: FishType[] = await this.findEntities(ctx);
for (const fishType of fishTypes) {
const fishBatchesCount: number = await ctx.call('fishBatches.count', {
query: {
fishType: fishType.id,
},
});
await this.updateEntity(ctx, { id: fishType.id, priority: fishBatchesCount });
}
}
}
32 changes: 31 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,14 @@
mkdirp "^1.0.4"
rimraf "^3.0.2"

"@r2d2bzh/moleculer-cron@^0.1.4":
version "0.1.4"
resolved "https://registry.npmjs.org/@r2d2bzh/moleculer-cron/-/moleculer-cron-0.1.4.tgz#74a75c566d62a6bf81339ff41b644d9caf9bb207"
integrity sha512-N9NE1hY9zfMOZ6B8MvHcCyCgwz678aKzd3gXDWJu4P6+GHgFSn4DrDxbxSYoy4LYl5HQWKkp03rmtTr/DO+nig==
dependencies:
cron "^2.2.0"
uuid "^9.0.0"

"@seald-io/binary-search-tree@^1.0.2":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@seald-io/binary-search-tree/-/binary-search-tree-1.0.3.tgz#165a9a456eaa30d15885b25db83861bcce2c6a74"
Expand Down Expand Up @@ -910,6 +918,11 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.202.tgz#f09dbd2fb082d507178b2f2a5c7e74bd72ff98f8"
integrity sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==

"@types/luxon@~3.3.0":
version "3.3.8"
resolved "https://registry.npmjs.org/@types/luxon/-/luxon-3.3.8.tgz#84dbf2d020a9209a272058725e168f21d331a67e"
integrity sha512-jYvz8UMLDgy3a5SkGJne8H7VA7zPV2Lwohjx0V8V31+SqAjNmurWMkk9cQhfvlcnXWudBpK9xPM1n4rljOcHYQ==

"@types/mime-types@^2.1.1":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@types/mime-types/-/mime-types-2.1.4.tgz#93a1933e24fed4fb9e4adc5963a63efcbb3317a2"
Expand Down Expand Up @@ -1433,7 +1446,6 @@ base64id@2.0.0:
resolved "https://codeload.github.com/dadpatch/biip-auth-nodejs/tar.gz/45a9e7d0051bde0e247d735948faf16c967e2422"
dependencies:
isomorphic-unfetch "^3.1.0"
qs "^6.11.2"

bl@^4.1.0:
version "4.1.0"
Expand Down Expand Up @@ -1864,6 +1876,14 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cron@^2.2.0:
version "2.4.4"
resolved "https://registry.npmjs.org/cron/-/cron-2.4.4.tgz#988c1757b3f288d1dfcc360ee6d80087448916dc"
integrity sha512-MHlPImXJj3K7x7lyUHjtKEOl69CSlTOWxS89jiFgNkzXfvhVjhMz/nc7/EIfN9vgooZp8XTtXJ1FREdmbyXOiQ==
dependencies:
"@types/luxon" "~3.3.0"
luxon "~3.3.0"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down Expand Up @@ -3994,6 +4014,11 @@ lru-queue@0.1:
dependencies:
es5-ext "~0.10.2"

luxon@~3.3.0:
version "3.3.0"
resolved "https://registry.npmjs.org/luxon/-/luxon-3.3.0.tgz#d73ab5b5d2b49a461c47cedbc7e73309b4805b48"
integrity sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==

make-dir@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
Expand Down Expand Up @@ -5870,6 +5895,11 @@ util@^0.12.3, util@^0.12.4:
is-typed-array "^1.1.3"
which-typed-array "^1.1.2"

uuid@^9.0.0:
version "9.0.1"
resolved "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==

v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
Expand Down

0 comments on commit e6d65f2

Please sign in to comment.