@things-factory/figure-service 10.1.69 → 10.1.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-server/service/figure-recipe/figure-recipe-resolver.d.ts +2 -0
- package/dist-server/service/figure-recipe/figure-recipe-resolver.js +25 -0
- package/dist-server/service/figure-recipe/figure-recipe-resolver.js.map +1 -1
- package/dist-server/service/figure-recipe/figure-recipe-store.d.ts +11 -0
- package/dist-server/service/figure-recipe/figure-recipe-store.js +26 -0
- package/dist-server/service/figure-recipe/figure-recipe-store.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/server/service/figure-recipe/figure-recipe-db.test.ts +20 -0
- package/server/service/figure-recipe/figure-recipe-resolver.ts +17 -1
- package/server/service/figure-recipe/figure-recipe-store.ts +26 -0
|
@@ -2,4 +2,6 @@ import { FigureRecipe } from './figure-recipe.js';
|
|
|
2
2
|
export declare class FigureRecipeResolver {
|
|
3
3
|
figureRecipes(all: boolean, context: ResolverContext): Promise<FigureRecipe[]>;
|
|
4
4
|
saveFigureRecipe(source: string, state: string, context: ResolverContext): Promise<FigureRecipe>;
|
|
5
|
+
setFigureRecipeState(name: string, state: string, context: ResolverContext): Promise<FigureRecipe>;
|
|
6
|
+
deleteFigureRecipe(name: string, context: ResolverContext): Promise<boolean>;
|
|
5
7
|
}
|
|
@@ -13,6 +13,12 @@ let FigureRecipeResolver = class FigureRecipeResolver {
|
|
|
13
13
|
async saveFigureRecipe(source, state, context) {
|
|
14
14
|
return await (0, figure_recipe_store_js_1.saveRecipe)(context.state.domain, source, state === 'draft' ? 'draft' : 'released');
|
|
15
15
|
}
|
|
16
|
+
async setFigureRecipeState(name, state, context) {
|
|
17
|
+
return await (0, figure_recipe_store_js_1.setRecipeState)(context.state.domain, name, state === 'draft' ? 'draft' : 'released');
|
|
18
|
+
}
|
|
19
|
+
async deleteFigureRecipe(name, context) {
|
|
20
|
+
return await (0, figure_recipe_store_js_1.deleteRecipe)(context.state.domain, name);
|
|
21
|
+
}
|
|
16
22
|
};
|
|
17
23
|
exports.FigureRecipeResolver = FigureRecipeResolver;
|
|
18
24
|
tslib_1.__decorate([
|
|
@@ -34,6 +40,25 @@ tslib_1.__decorate([
|
|
|
34
40
|
tslib_1.__metadata("design:paramtypes", [String, String, Object]),
|
|
35
41
|
tslib_1.__metadata("design:returntype", Promise)
|
|
36
42
|
], FigureRecipeResolver.prototype, "saveFigureRecipe", null);
|
|
43
|
+
tslib_1.__decorate([
|
|
44
|
+
(0, type_graphql_1.Directive)('@privilege(category: "figure", privilege: "mutation", domainOwnerGranted: true)'),
|
|
45
|
+
(0, type_graphql_1.Mutation)(returns => figure_recipe_js_1.FigureRecipe, { description: "Releases a recipe of this domain's own, or takes it back to draft. A parent's recipe is copied in to be changed." }),
|
|
46
|
+
tslib_1.__param(0, (0, type_graphql_1.Arg)('name', { description: 'The recipe name.' })),
|
|
47
|
+
tslib_1.__param(1, (0, type_graphql_1.Arg)('state', { description: "'draft' or 'released'." })),
|
|
48
|
+
tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
|
|
49
|
+
tslib_1.__metadata("design:type", Function),
|
|
50
|
+
tslib_1.__metadata("design:paramtypes", [String, String, Object]),
|
|
51
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
52
|
+
], FigureRecipeResolver.prototype, "setFigureRecipeState", null);
|
|
53
|
+
tslib_1.__decorate([
|
|
54
|
+
(0, type_graphql_1.Directive)('@privilege(category: "figure", privilege: "mutation", domainOwnerGranted: true)'),
|
|
55
|
+
(0, type_graphql_1.Mutation)(returns => Boolean, { description: "Deletes a recipe of this domain's own. Figures it was put into keep their parts." }),
|
|
56
|
+
tslib_1.__param(0, (0, type_graphql_1.Arg)('name', { description: 'The recipe name.' })),
|
|
57
|
+
tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
|
|
58
|
+
tslib_1.__metadata("design:type", Function),
|
|
59
|
+
tslib_1.__metadata("design:paramtypes", [String, Object]),
|
|
60
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
61
|
+
], FigureRecipeResolver.prototype, "deleteFigureRecipe", null);
|
|
37
62
|
exports.FigureRecipeResolver = FigureRecipeResolver = tslib_1.__decorate([
|
|
38
63
|
(0, type_graphql_1.Resolver)(figure_recipe_js_1.FigureRecipe),
|
|
39
64
|
(0, type_graphql_1.UseMiddleware)(shell_1.SpeakRefusals)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"figure-recipe-resolver.js","sourceRoot":"","sources":["../../../server/service/figure-recipe/figure-recipe-resolver.ts"],"names":[],"mappings":";;;;AAAA,+CAA4F;AAE5F,iDAAqD;AAErD,yDAAiD;AACjD,
|
|
1
|
+
{"version":3,"file":"figure-recipe-resolver.js","sourceRoot":"","sources":["../../../server/service/figure-recipe/figure-recipe-resolver.ts"],"names":[],"mappings":";;;;AAAA,+CAA4F;AAE5F,iDAAqD;AAErD,yDAAiD;AACjD,qEAA+F;AAIxF,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAGzB,AAAN,KAAK,CAAC,aAAa,CAAiE,GAAY,EAAS,OAAwB;QAC/H,OAAO,MAAM,IAAA,mCAAU,EAAC,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAA;IAC7E,CAAC;IAIK,AAAN,KAAK,CAAC,gBAAgB,CACwD,MAAc,EACH,KAAa,EAC7F,OAAwB;QAE/B,OAAO,MAAM,IAAA,mCAAU,EAAC,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,CAAC,CAAA;IACjG,CAAC;IAIK,AAAN,KAAK,CAAC,oBAAoB,CAC0B,IAAY,EACL,KAAa,EAC/D,OAAwB;QAE/B,OAAO,MAAM,IAAA,uCAAc,EAAC,OAA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EAAC,MAAM,EAAE,EAAE,WAAW,EAAE,kBAAkB,EAAE,CAAC,CAAA;IAChD,mBAAA,IAAA,kBAAG,EAAC,OAAO,EAAE,EAAE,WAAW,EAAE,wBAAwB,EAAE,CAAC,CAAA;IACvD,mBAAA,IAAA,kBAAG,GAAE,CAAA;;;;gEAGP;AAIK;IAFL,IAAA,wBAAS,EAAC,iFAAiF,CAAC;IAC5F,IAAA,uBAAQ,EAAC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,kFAAkF,EAAE,CAAC;IACxG,mBAAA,IAAA,kBAAG,EAAC,MAAM,EAAE,EAAE,WAAW,EAAE,kBAAkB,EAAE,CAAC,CAAA;IAAgB,mBAAA,IAAA,kBAAG,GAAE,CAAA;;;;8DAE9F;+BA/BU,oBAAoB;IAFhC,IAAA,uBAAQ,EAAC,+BAAY,CAAC;IACtB,IAAA,4BAAa,EAAC,qBAAa,CAAC;GAChB,oBAAoB,CAgChC","sourcesContent":["import { Arg, Ctx, Directive, Mutation, Query, Resolver, UseMiddleware } from 'type-graphql'\n\nimport { SpeakRefusals } from '@things-factory/shell'\n\nimport { FigureRecipe } from './figure-recipe.js'\nimport { deleteRecipe, recipesFor, saveRecipe, setRecipeState } from './figure-recipe-store.js'\n\n@Resolver(FigureRecipe)\n@UseMiddleware(SpeakRefusals)\nexport class FigureRecipeResolver {\n @Directive('@privilege(category: \"figure\", privilege: \"query\", domainOwnerGranted: true)')\n @Query(returns => [FigureRecipe], { description: \"The recipes this domain sees: its own and its parent's (its own stand in by name). Released ones unless all is asked.\" })\n async figureRecipes(@Arg('all', { nullable: true, description: 'Drafts as well.' }) all: boolean, @Ctx() context: ResolverContext): Promise<FigureRecipe[]> {\n return await recipesFor(context.state.domain, all ? undefined : 'released')\n }\n\n @Directive('@privilege(category: \"figure\", privilege: \"mutation\", domainOwnerGranted: true)')\n @Mutation(returns => FigureRecipe, { description: 'Saves a recipe into this domain by its name, after putting it into an empty room with its defaults (ADR-0095).' })\n async saveFigureRecipe(\n @Arg('source', { description: 'The recipe JSON (figure-model V3Recipe).' }) source: string,\n @Arg('state', { nullable: true, description: \"'draft' or 'released' (the default).\" }) state: string,\n @Ctx() context: ResolverContext\n ): Promise<FigureRecipe> {\n return await saveRecipe(context.state.domain, source, state === 'draft' ? 'draft' : 'released')\n }\n\n @Directive('@privilege(category: \"figure\", privilege: \"mutation\", domainOwnerGranted: true)')\n @Mutation(returns => FigureRecipe, { description: \"Releases a recipe of this domain's own, or takes it back to draft. A parent's recipe is copied in to be changed.\" })\n async setFigureRecipeState(\n @Arg('name', { description: 'The recipe name.' }) name: string,\n @Arg('state', { description: \"'draft' or 'released'.\" }) state: string,\n @Ctx() context: ResolverContext\n ): Promise<FigureRecipe> {\n return await setRecipeState(context.state.domain, name, state === 'draft' ? 'draft' : 'released')\n }\n\n @Directive('@privilege(category: \"figure\", privilege: \"mutation\", domainOwnerGranted: true)')\n @Mutation(returns => Boolean, { description: \"Deletes a recipe of this domain's own. Figures it was put into keep their parts.\" })\n async deleteFigureRecipe(@Arg('name', { description: 'The recipe name.' }) name: string, @Ctx() context: ResolverContext): Promise<boolean> {\n return await deleteRecipe(context.state.domain, name)\n }\n}\n"]}
|
|
@@ -13,6 +13,17 @@ export declare function recipesFor(domain: {
|
|
|
13
13
|
* (figure-model `checkV3Recipe`). Refused, it is not saved and the kernel's sentence goes back as it is.
|
|
14
14
|
*/
|
|
15
15
|
export declare function saveRecipe(domain: Domain, source: unknown, state?: 'draft' | 'released'): Promise<FigureRecipe>;
|
|
16
|
+
/** Released or back to draft: only released recipes are offered in the modeller. */
|
|
17
|
+
export declare function setRecipeState(domain: {
|
|
18
|
+
id: string;
|
|
19
|
+
}, name: string, state: 'draft' | 'released'): Promise<FigureRecipe>;
|
|
20
|
+
/**
|
|
21
|
+
* A recipe of the domain's own taken away. Figures it was put into keep their parts -- nothing ties them to it
|
|
22
|
+
* (ADR-0095). Where the parent has one by that name, the parent's is seen again.
|
|
23
|
+
*/
|
|
24
|
+
export declare function deleteRecipe(domain: {
|
|
25
|
+
id: string;
|
|
26
|
+
}, name: string): Promise<boolean>;
|
|
16
27
|
/** The recipes figure-model ships (`recipes/*.json`), as they are. */
|
|
17
28
|
export declare function builtInRecipes(): Array<{
|
|
18
29
|
name: string;
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.recipesFor = recipesFor;
|
|
4
4
|
exports.saveRecipe = saveRecipe;
|
|
5
|
+
exports.setRecipeState = setRecipeState;
|
|
6
|
+
exports.deleteRecipe = deleteRecipe;
|
|
5
7
|
exports.builtInRecipes = builtInRecipes;
|
|
6
8
|
exports.seedBuiltInRecipes = seedBuiltInRecipes;
|
|
7
9
|
const fs_1 = require("fs");
|
|
@@ -39,6 +41,30 @@ async function saveRecipe(domain, source, state = 'released') {
|
|
|
39
41
|
const existing = await repository.findOne({ where: { domain: { id: domain.id }, name: recipe.name } });
|
|
40
42
|
return await repository.save({ ...(existing ?? {}), domain, name: recipe.name, source: JSON.stringify(recipe), state });
|
|
41
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* A recipe of the domain's own, by name. A parent's recipe is not the domain's to change: it is copied in under the same
|
|
46
|
+
* name (`saveRecipe`), and the copy stands in for it.
|
|
47
|
+
*/
|
|
48
|
+
async function ownRecipe(domain, name) {
|
|
49
|
+
const row = await (0, shell_1.getRepository)(figure_recipe_js_1.FigureRecipe).findOne({ where: { domain: { id: domain.id }, name } });
|
|
50
|
+
if (!row)
|
|
51
|
+
throw new Error(`${name} is not a recipe of this domain; a parent's recipe is copied here to be changed`);
|
|
52
|
+
return row;
|
|
53
|
+
}
|
|
54
|
+
/** Released or back to draft: only released recipes are offered in the modeller. */
|
|
55
|
+
async function setRecipeState(domain, name, state) {
|
|
56
|
+
const row = await ownRecipe(domain, name);
|
|
57
|
+
return await (0, shell_1.getRepository)(figure_recipe_js_1.FigureRecipe).save({ ...row, state });
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A recipe of the domain's own taken away. Figures it was put into keep their parts -- nothing ties them to it
|
|
61
|
+
* (ADR-0095). Where the parent has one by that name, the parent's is seen again.
|
|
62
|
+
*/
|
|
63
|
+
async function deleteRecipe(domain, name) {
|
|
64
|
+
const row = await ownRecipe(domain, name);
|
|
65
|
+
await (0, shell_1.getRepository)(figure_recipe_js_1.FigureRecipe).softRemove(row);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
42
68
|
/** The recipes figure-model ships (`recipes/*.json`), as they are. */
|
|
43
69
|
function builtInRecipes() {
|
|
44
70
|
const dir = (0, path_1.join)((0, path_1.dirname)(require.resolve('@hatiolab/figure-model/package.json')), 'recipes');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"figure-recipe-store.js","sourceRoot":"","sources":["../../../server/service/figure-recipe/figure-recipe-store.ts"],"names":[],"mappings":";;AAaA,gCAaC;AAMD,gCAKC;AAGD,wCASC;AAOD,gDAkBC;
|
|
1
|
+
{"version":3,"file":"figure-recipe-store.js","sourceRoot":"","sources":["../../../server/service/figure-recipe/figure-recipe-store.ts"],"names":[],"mappings":";;AAaA,gCAaC;AAMD,gCAKC;AAaD,wCAGC;AAMD,oCAIC;AAGD,wCASC;AAOD,gDAkBC;AApGD,2BAA8C;AAC9C,+BAAoC;AACpC,qCAA4B;AAE5B,yDAAsD;AACtD,iDAA6D;AAE7D,yDAAiD;AAEjD;;;GAGG;AACI,KAAK,UAAU,UAAU,CAAC,MAAgD,EAAE,KAAkB;IACnG,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAa,CAAA;IACpE,MAAM,IAAI,GAAG,MAAM,IAAA,qBAAa,EAAC,+BAAY,CAAC,CAAC,IAAI,CAAC;QAClD,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,IAAA,YAAE,EAAC,GA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AM,CAAC,CAAA;YAChC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,KAAK,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;QAC3F,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import { readdirSync, readFileSync } from 'fs'\nimport { dirname, join } from 'path'\nimport { In } from 'typeorm'\n\nimport { checkV3Recipe } from '@hatiolab/figure-model'\nimport { Domain, getRepository } from '@things-factory/shell'\n\nimport { FigureRecipe } from './figure-recipe.js'\n\n/**\n * The recipes a domain sees (ADR-0095 condition 2): its own and its parent's, as figures are read. A recipe of the\n * domain's own stands in for the parent's of the same name.\n */\nexport async function recipesFor(domain: { id: string; parentId?: string | null }, state?: 'released'): Promise<FigureRecipe[]> {\n const ids = [domain.id, domain.parentId].filter(Boolean) as string[]\n const rows = await getRepository(FigureRecipe).find({\n where: { domain: { id: In(ids) }, ...(state ? { state } : {}) },\n relations: ['domain'],\n order: { name: 'ASC' }\n })\n const byName = new Map<string, FigureRecipe>()\n for (const row of rows) {\n const mine = row.domainId === domain.id || (row.domain as Domain | undefined)?.id === domain.id\n if (mine || !byName.has(row.name!)) byName.set(row.name!, row)\n }\n return [...byName.values()]\n}\n\n/**\n * A recipe saved into a domain, by its name: checked first by putting it into the test room with its defaults\n * (figure-model `checkV3Recipe`). Refused, it is not saved and the kernel's sentence goes back as it is.\n */\nexport async function saveRecipe(domain: Domain, source: unknown, state: 'draft' | 'released' = 'released'): Promise<FigureRecipe> {\n const recipe = checkV3Recipe(typeof source === 'string' ? JSON.parse(source) : source)\n const repository = getRepository(FigureRecipe)\n const existing = await repository.findOne({ where: { domain: { id: domain.id }, name: recipe.name } })\n return await repository.save({ ...(existing ?? {}), domain, name: recipe.name, source: JSON.stringify(recipe), state })\n}\n\n/**\n * A recipe of the domain's own, by name. A parent's recipe is not the domain's to change: it is copied in under the same\n * name (`saveRecipe`), and the copy stands in for it.\n */\nasync function ownRecipe(domain: { id: string }, name: string): Promise<FigureRecipe> {\n const row = await getRepository(FigureRecipe).findOne({ where: { domain: { id: domain.id }, name } })\n if (!row) throw new Error(`${name} is not a recipe of this domain; a parent's recipe is copied here to be changed`)\n return row\n}\n\n/** Released or back to draft: only released recipes are offered in the modeller. */\nexport async function setRecipeState(domain: { id: string }, name: string, state: 'draft' | 'released'): Promise<FigureRecipe> {\n const row = await ownRecipe(domain, name)\n return await getRepository(FigureRecipe).save({ ...row, state })\n}\n\n/**\n * A recipe of the domain's own taken away. Figures it was put into keep their parts -- nothing ties them to it\n * (ADR-0095). Where the parent has one by that name, the parent's is seen again.\n */\nexport async function deleteRecipe(domain: { id: string }, name: string): Promise<boolean> {\n const row = await ownRecipe(domain, name)\n await getRepository(FigureRecipe).softRemove(row)\n return true\n}\n\n/** The recipes figure-model ships (`recipes/*.json`), as they are. */\nexport function builtInRecipes(): Array<{ name: string; source: string }> {\n const dir = join(dirname(require.resolve('@hatiolab/figure-model/package.json')), 'recipes')\n return readdirSync(dir)\n .filter(file => file.endsWith('.json'))\n .sort()\n .map(file => {\n const source = readFileSync(join(dir, file), 'utf-8')\n return { name: JSON.parse(source).name, source }\n })\n}\n\n/**\n * The built-in recipes put into the system domain (ADR-0095 condition 2): once, and again gives the same result -- a\n * recipe already there by name is skipped, not overwritten, so a system recipe someone changed stays as they left it.\n * Each is checked as a saved one is; one that fails is left out and said, not half put in.\n */\nexport async function seedBuiltInRecipes(): Promise<{ added: string[]; skipped: string[]; refused: string[] }> {\n const system = await getRepository(Domain).findOne({ where: { systemFlag: true } })\n const result = { added: [] as string[], skipped: [] as string[], refused: [] as string[] }\n if (!system) return result\n const repository = getRepository(FigureRecipe)\n for (const { name, source } of builtInRecipes()) {\n if (await repository.count({ where: { domain: { id: system.id }, name } })) {\n result.skipped.push(name)\n continue\n }\n try {\n await saveRecipe(system, source)\n result.added.push(name)\n } catch (error) {\n result.refused.push(`${name}: ${error instanceof Error ? error.message : String(error)}`)\n }\n }\n return result\n}\n"]}
|