@aneuhold/be-ts-db-lib 1.0.115 → 2.0.20
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/LICENSE +21 -0
- package/README.md +46 -10
- package/lib/index.d.ts +8 -8
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +10 -20
- package/lib/index.js.map +1 -0
- package/lib/index.ts +21 -0
- package/lib/repositories/BaseRepository.d.ts +84 -11
- package/lib/repositories/BaseRepository.d.ts.map +1 -1
- package/lib/repositories/BaseRepository.js +89 -21
- package/lib/repositories/BaseRepository.js.map +1 -0
- package/lib/repositories/BaseRepository.ts +339 -0
- package/lib/repositories/common/ApiKeyRepository.d.ts +2 -2
- package/lib/repositories/common/ApiKeyRepository.d.ts.map +1 -1
- package/lib/repositories/common/ApiKeyRepository.js +8 -13
- package/lib/repositories/common/ApiKeyRepository.js.map +1 -0
- package/lib/repositories/common/ApiKeyRepository.ts +55 -0
- package/lib/repositories/common/UserRepository.d.ts +1 -1
- package/lib/repositories/common/UserRepository.d.ts.map +1 -1
- package/lib/repositories/common/UserRepository.js +15 -20
- package/lib/repositories/common/UserRepository.js.map +1 -0
- package/lib/repositories/common/UserRepository.ts +65 -0
- package/lib/repositories/dashboard/DashboardBaseRepository.d.ts +2 -2
- package/lib/repositories/dashboard/DashboardBaseRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardBaseRepository.js +6 -11
- package/lib/repositories/dashboard/DashboardBaseRepository.js.map +1 -0
- package/lib/repositories/dashboard/DashboardBaseRepository.ts +31 -0
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.d.ts +2 -2
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.js +7 -12
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.js.map +1 -0
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.ts +64 -0
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.d.ts +2 -2
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.js +7 -12
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.js.map +1 -0
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.ts +66 -0
- package/lib/repositories/dashboard/DashboardTaskRepository.d.ts +17 -4
- package/lib/repositories/dashboard/DashboardTaskRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardTaskRepository.js +26 -18
- package/lib/repositories/dashboard/DashboardTaskRepository.js.map +1 -0
- package/lib/repositories/dashboard/DashboardTaskRepository.ts +166 -0
- package/lib/repositories/dashboard/DashboardUserConfigRepository.d.ts +29 -5
- package/lib/repositories/dashboard/DashboardUserConfigRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardUserConfigRepository.js +38 -19
- package/lib/repositories/dashboard/DashboardUserConfigRepository.js.map +1 -0
- package/lib/repositories/dashboard/DashboardUserConfigRepository.ts +254 -0
- package/lib/services/MigrationService.d.ts.map +1 -1
- package/lib/services/MigrationService.js +10 -15
- package/lib/services/MigrationService.js.map +1 -0
- package/lib/services/MigrationService.ts +52 -0
- package/lib/services/RepoSubscriptionService.d.ts.map +1 -1
- package/lib/services/RepoSubscriptionService.js +2 -4
- package/lib/services/RepoSubscriptionService.js.map +1 -0
- package/lib/services/RepoSubscriptionService.ts +63 -0
- package/lib/tests/globalTestVariables.d.ts +5 -0
- package/lib/tests/globalTestVariables.d.ts.map +1 -0
- package/lib/tests/globalTestVariables.js +5 -0
- package/lib/tests/globalTestVariables.js.map +1 -0
- package/lib/tests/globalTestVariables.ts +4 -0
- package/lib/tests/repositories/BaseRepository.spec.ts +61 -0
- package/lib/tests/repositories/common/UserRepository.spec.ts +113 -0
- package/lib/tests/repositories/dashboard/NonogramKatanaItemRepository.spec.ts +55 -0
- package/lib/tests/repositories/dashboard/NonogramKatanaUpgradeRepository.spec.ts +14 -0
- package/lib/tests/repositories/dashboard/TaskRepository.spec.ts +101 -0
- package/lib/tests/repositories/dashboard/UserConfigRepository.spec.ts +155 -0
- package/lib/tests/testsUtil.d.ts +20 -4
- package/lib/tests/testsUtil.d.ts.map +1 -1
- package/lib/tests/testsUtil.js +29 -20
- package/lib/tests/testsUtil.js.map +1 -0
- package/lib/tests/testsUtil.ts +68 -0
- package/lib/util/DbSchemaUpdater.d.ts.map +1 -1
- package/lib/util/DbSchemaUpdater.js +10 -15
- package/lib/util/DbSchemaUpdater.js.map +1 -0
- package/lib/util/DbSchemaUpdater.ts +16 -0
- package/lib/util/DocumentCleaner.d.ts.map +1 -1
- package/lib/util/DocumentCleaner.js +2 -4
- package/lib/util/DocumentCleaner.js.map +1 -0
- package/lib/util/DocumentCleaner.ts +31 -0
- package/lib/util/DocumentDb.d.ts +4 -1
- package/lib/util/DocumentDb.d.ts.map +1 -1
- package/lib/util/DocumentDb.js +11 -10
- package/lib/util/DocumentDb.js.map +1 -0
- package/lib/util/DocumentDb.ts +47 -0
- package/lib/validators/BaseValidator.d.ts +11 -0
- package/lib/validators/BaseValidator.d.ts.map +1 -1
- package/lib/validators/BaseValidator.js +26 -18
- package/lib/validators/BaseValidator.js.map +1 -0
- package/lib/validators/BaseValidator.ts +150 -0
- package/lib/validators/common/ApiKeyValidator.d.ts +1 -1
- package/lib/validators/common/ApiKeyValidator.d.ts.map +1 -1
- package/lib/validators/common/ApiKeyValidator.js +16 -21
- package/lib/validators/common/ApiKeyValidator.js.map +1 -0
- package/lib/validators/common/ApiKeyValidator.ts +67 -0
- package/lib/validators/common/UserValidator.d.ts +14 -1
- package/lib/validators/common/UserValidator.d.ts.map +1 -1
- package/lib/validators/common/UserValidator.js +29 -21
- package/lib/validators/common/UserValidator.js.map +1 -0
- package/lib/validators/common/UserValidator.ts +98 -0
- package/lib/validators/dashboard/NonogramKatanaItemValidator.d.ts +1 -1
- package/lib/validators/dashboard/NonogramKatanaItemValidator.d.ts.map +1 -1
- package/lib/validators/dashboard/NonogramKatanaItemValidator.js +16 -21
- package/lib/validators/dashboard/NonogramKatanaItemValidator.js.map +1 -0
- package/lib/validators/dashboard/NonogramKatanaItemValidator.ts +78 -0
- package/lib/validators/dashboard/NonogramKatanaUpgradeValidator.d.ts +1 -1
- package/lib/validators/dashboard/NonogramKatanaUpgradeValidator.d.ts.map +1 -1
- package/lib/validators/dashboard/NonogramKatanaUpgradeValidator.js +16 -21
- package/lib/validators/dashboard/NonogramKatanaUpgradeValidator.js.map +1 -0
- package/lib/validators/dashboard/NonogramKatanaUpgradeValidator.ts +78 -0
- package/lib/validators/dashboard/TaskValidator.d.ts +1 -1
- package/lib/validators/dashboard/TaskValidator.d.ts.map +1 -1
- package/lib/validators/dashboard/TaskValidator.js +15 -20
- package/lib/validators/dashboard/TaskValidator.js.map +1 -0
- package/lib/validators/dashboard/TaskValidator.ts +105 -0
- package/lib/validators/dashboard/UserConfigValidator.d.ts +1 -1
- package/lib/validators/dashboard/UserConfigValidator.d.ts.map +1 -1
- package/lib/validators/dashboard/UserConfigValidator.js +19 -24
- package/lib/validators/dashboard/UserConfigValidator.js.map +1 -0
- package/lib/validators/dashboard/UserConfigValidator.ts +112 -0
- package/package.json +47 -41
- package/lib/scripts/migrate.d.ts +0 -2
- package/lib/scripts/migrate.d.ts.map +0 -1
- package/lib/scripts/migrate.js +0 -7
- package/lib/scripts/migrateDry.d.ts +0 -2
- package/lib/scripts/migrateDry.d.ts.map +0 -1
- package/lib/scripts/migrateDry.js +0 -7
- package/lib/scripts/validateSchema.d.ts +0 -2
- package/lib/scripts/validateSchema.d.ts.map +0 -1
- package/lib/scripts/validateSchema.js +0 -9
- package/lib/scripts/validateSchemaDryRun.d.ts +0 -2
- package/lib/scripts/validateSchemaDryRun.d.ts.map +0 -1
- package/lib/scripts/validateSchemaDryRun.js +0 -9
- package/lib/tests/repositories/BaseRepository.spec.d.ts +0 -2
- package/lib/tests/repositories/BaseRepository.spec.d.ts.map +0 -1
- package/lib/tests/repositories/BaseRepository.spec.js +0 -52
- package/lib/tests/repositories/common/UserRepository.spec.d.ts +0 -2
- package/lib/tests/repositories/common/UserRepository.spec.d.ts.map +0 -1
- package/lib/tests/repositories/common/UserRepository.spec.js +0 -108
- package/lib/tests/repositories/dashboard/NonogramKatanaItemRepository.spec.d.ts +0 -2
- package/lib/tests/repositories/dashboard/NonogramKatanaItemRepository.spec.d.ts.map +0 -1
- package/lib/tests/repositories/dashboard/NonogramKatanaItemRepository.spec.js +0 -39
- package/lib/tests/repositories/dashboard/NonogramKatanaUpgradeRepository.spec.d.ts +0 -2
- package/lib/tests/repositories/dashboard/NonogramKatanaUpgradeRepository.spec.d.ts.map +0 -1
- package/lib/tests/repositories/dashboard/NonogramKatanaUpgradeRepository.spec.js +0 -16
- package/lib/tests/repositories/dashboard/TaskRepository.spec.d.ts +0 -2
- package/lib/tests/repositories/dashboard/TaskRepository.spec.d.ts.map +0 -1
- package/lib/tests/repositories/dashboard/TaskRepository.spec.js +0 -84
- package/lib/tests/repositories/dashboard/UserConfigRepository.spec.d.ts +0 -2
- package/lib/tests/repositories/dashboard/UserConfigRepository.spec.d.ts.map +0 -1
- package/lib/tests/repositories/dashboard/UserConfigRepository.spec.js +0 -115
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { BaseDocument, DocumentValidator } from '@aneuhold/core-ts-db-lib';
|
|
2
|
+
import { Logger } from '@aneuhold/core-ts-lib';
|
|
3
|
+
import { ObjectId } from 'bson';
|
|
4
|
+
|
|
5
|
+
export enum ObjectSchemaState {
|
|
6
|
+
Valid,
|
|
7
|
+
InvalidAndCorrectable,
|
|
8
|
+
InvalidAndUncorrectable
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default abstract class IValidator<TBaseType extends BaseDocument> {
|
|
12
|
+
/**
|
|
13
|
+
* Validates that an object that is supposed to be inserted in to the database
|
|
14
|
+
* is correct.
|
|
15
|
+
*/
|
|
16
|
+
abstract validateNewObject(object: TBaseType): Promise<void>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Validates an object that is suppposed to be updated in the database.
|
|
20
|
+
*
|
|
21
|
+
* At this point, the fields that do not change should already be stripped.
|
|
22
|
+
*/
|
|
23
|
+
abstract validateUpdateObject(
|
|
24
|
+
partialObject: Partial<TBaseType>
|
|
25
|
+
): Promise<void>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Validates the entire DB for the repository, and corrects where needed.
|
|
29
|
+
*
|
|
30
|
+
* This should only correct the records that this repository has a full
|
|
31
|
+
* understanding of.
|
|
32
|
+
*
|
|
33
|
+
* This can be a long-running process, so it should only be executed in a
|
|
34
|
+
* script.
|
|
35
|
+
*
|
|
36
|
+
* Make sure to use the {@link runStandardValidationForRepository} method
|
|
37
|
+
* to run the validation to make things easier.
|
|
38
|
+
*/
|
|
39
|
+
abstract validateRepositoryInDb(dryRun: boolean): Promise<void>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Runs the standard validation for a repository.
|
|
43
|
+
*
|
|
44
|
+
* @param input
|
|
45
|
+
* @param shouldDelete A function that returns true if the document should be
|
|
46
|
+
* deleted. This should also log the specific error because it will not be
|
|
47
|
+
* logged elsewhere.
|
|
48
|
+
* @param input.dryRun
|
|
49
|
+
* @param input.docName
|
|
50
|
+
* @param input.allDocs
|
|
51
|
+
* @param input.shouldDelete
|
|
52
|
+
* @param input.documentValidator
|
|
53
|
+
* @param input.deletionFunction
|
|
54
|
+
* @param input.updateFunction
|
|
55
|
+
*/
|
|
56
|
+
protected async runStandardValidationForRepository(input: {
|
|
57
|
+
dryRun: boolean;
|
|
58
|
+
docName: string;
|
|
59
|
+
allDocs: Array<TBaseType>;
|
|
60
|
+
shouldDelete: (doc: TBaseType) => boolean;
|
|
61
|
+
documentValidator: DocumentValidator<TBaseType>;
|
|
62
|
+
deletionFunction: (docIdsToDelete: ObjectId[]) => Promise<void>;
|
|
63
|
+
updateFunction: (docsToUpdate: TBaseType[]) => Promise<void>;
|
|
64
|
+
}) {
|
|
65
|
+
const {
|
|
66
|
+
dryRun,
|
|
67
|
+
docName,
|
|
68
|
+
allDocs,
|
|
69
|
+
shouldDelete,
|
|
70
|
+
documentValidator,
|
|
71
|
+
deletionFunction,
|
|
72
|
+
updateFunction
|
|
73
|
+
} = input;
|
|
74
|
+
const docIdsToDelete: Array<ObjectId> = [];
|
|
75
|
+
const docsToValidate: Array<TBaseType> = [];
|
|
76
|
+
const docsToUpdate: Array<TBaseType> = [];
|
|
77
|
+
let numInvalidDocs = 0;
|
|
78
|
+
|
|
79
|
+
// Check for docs that need to be deleted
|
|
80
|
+
allDocs.forEach((doc) => {
|
|
81
|
+
if (shouldDelete(doc)) {
|
|
82
|
+
docIdsToDelete.push(doc._id);
|
|
83
|
+
} else {
|
|
84
|
+
docsToValidate.push(doc);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
// Validate the rest
|
|
88
|
+
docsToValidate.forEach((doc) => {
|
|
89
|
+
const { updatedDoc, errors } = documentValidator(doc);
|
|
90
|
+
if (errors.length !== 0) {
|
|
91
|
+
Logger.error(
|
|
92
|
+
`${docName} with ID: ${doc._id.toString()} is invalid. Errors:`
|
|
93
|
+
);
|
|
94
|
+
numInvalidDocs += 1;
|
|
95
|
+
errors.forEach((error) => {
|
|
96
|
+
Logger.error(error);
|
|
97
|
+
});
|
|
98
|
+
docsToUpdate.push(updatedDoc);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
if (dryRun) {
|
|
102
|
+
if (numInvalidDocs === 0) {
|
|
103
|
+
Logger.success(`No invalid ${docName}s found.`);
|
|
104
|
+
} else {
|
|
105
|
+
Logger.info(
|
|
106
|
+
`Would update ${numInvalidDocs} ${docName}s in the database.`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (docIdsToDelete.length === 0) {
|
|
110
|
+
Logger.success(`No ${docName}s to delete found.`);
|
|
111
|
+
} else {
|
|
112
|
+
Logger.info(
|
|
113
|
+
`Would delete ${docIdsToDelete.length} ${docName}s in the database.`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
// Delete all invalid
|
|
119
|
+
if (docIdsToDelete.length !== 0) {
|
|
120
|
+
Logger.info(
|
|
121
|
+
`Deleting ${docIdsToDelete.length} ${docName}s from the database.`
|
|
122
|
+
);
|
|
123
|
+
await deletionFunction(docIdsToDelete);
|
|
124
|
+
} else {
|
|
125
|
+
Logger.success(`No ${docName}s to delete found.`);
|
|
126
|
+
}
|
|
127
|
+
// Update all that need to be updated
|
|
128
|
+
if (docsToUpdate.length !== 0) {
|
|
129
|
+
Logger.info(
|
|
130
|
+
`Updating ${docsToUpdate.length} ${docName}s in the database.`
|
|
131
|
+
);
|
|
132
|
+
await updateFunction(docsToUpdate);
|
|
133
|
+
} else {
|
|
134
|
+
Logger.success(`No ${docName}s to update found.`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Checks that all elements that exist in array1, exist in array2.
|
|
140
|
+
*
|
|
141
|
+
* @param array1
|
|
142
|
+
* @param array2
|
|
143
|
+
*/
|
|
144
|
+
protected checkAllElementsExistInArr(
|
|
145
|
+
array1: Array<unknown>,
|
|
146
|
+
array2: Array<unknown>
|
|
147
|
+
): boolean {
|
|
148
|
+
return array1.every((value) => array2.includes(value));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ApiKey } from '@aneuhold/core-ts-db-lib';
|
|
2
|
-
import IValidator from '../BaseValidator';
|
|
2
|
+
import IValidator from '../BaseValidator.js';
|
|
3
3
|
export default class ApiKeyValidator extends IValidator<ApiKey> {
|
|
4
4
|
validateNewObject(newApiKey: ApiKey): Promise<void>;
|
|
5
5
|
validateUpdateObject(updatedApiKey: Partial<ApiKey>): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiKeyValidator.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"ApiKeyValidator.d.ts","sourceRoot":"./src/","sources":["validators/common/ApiKeyValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAkB,MAAM,0BAA0B,CAAC;AAKlE,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAE7C,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,UAAU,CAAC,MAAM,CAAC;IACvD,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBnD,oBAAoB,CAAC,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAQnE,sBAAsB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CA2B7D"}
|
|
@@ -1,50 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const ApiKey_1 = require("@aneuhold/core-ts-db-lib/lib/documents/common/ApiKey");
|
|
8
|
-
const UserRepository_1 = __importDefault(require("../../repositories/common/UserRepository"));
|
|
9
|
-
const BaseValidator_1 = __importDefault(require("../BaseValidator"));
|
|
10
|
-
const ApiKeyRepository_1 = __importDefault(require("../../repositories/common/ApiKeyRepository"));
|
|
11
|
-
class ApiKeyValidator extends BaseValidator_1.default {
|
|
1
|
+
import { validateApiKey } from '@aneuhold/core-ts-db-lib';
|
|
2
|
+
import { ErrorUtils, Logger } from '@aneuhold/core-ts-lib';
|
|
3
|
+
import ApiKeyRepository from '../../repositories/common/ApiKeyRepository.js';
|
|
4
|
+
import UserRepository from '../../repositories/common/UserRepository.js';
|
|
5
|
+
import IValidator from '../BaseValidator.js';
|
|
6
|
+
export default class ApiKeyValidator extends IValidator {
|
|
12
7
|
async validateNewObject(newApiKey) {
|
|
13
8
|
// Check if the user exists
|
|
14
|
-
const userRepo =
|
|
9
|
+
const userRepo = UserRepository.getRepo();
|
|
15
10
|
const userInDb = await userRepo.get({ _id: newApiKey.userId });
|
|
16
11
|
if (!userInDb) {
|
|
17
|
-
|
|
12
|
+
ErrorUtils.throwError(`User with ID: ${newApiKey.userId.toString()} does not exist in the database.`, newApiKey);
|
|
18
13
|
return;
|
|
19
14
|
}
|
|
20
15
|
// Check if the user already has an API key
|
|
21
|
-
const apiKeyRepo =
|
|
16
|
+
const apiKeyRepo = ApiKeyRepository.getRepo();
|
|
22
17
|
const apiKeyInDb = await apiKeyRepo.get({ userId: newApiKey.userId });
|
|
23
18
|
if (apiKeyInDb) {
|
|
24
|
-
|
|
19
|
+
ErrorUtils.throwError(`User with ID: ${newApiKey.userId.toString()} already has an API key.`, newApiKey);
|
|
25
20
|
}
|
|
26
21
|
}
|
|
27
22
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
28
23
|
async validateUpdateObject(updatedApiKey) {
|
|
29
24
|
// Throw, because API keys should not be updated. Only created and deleted.
|
|
30
|
-
|
|
25
|
+
ErrorUtils.throwError(`API keys should not be updated at this time. Only created and deleted.`, updatedApiKey);
|
|
31
26
|
}
|
|
32
27
|
async validateRepositoryInDb(dryRun) {
|
|
33
|
-
const apiKeyRepo =
|
|
28
|
+
const apiKeyRepo = ApiKeyRepository.getRepo();
|
|
34
29
|
const allApiKeys = await apiKeyRepo.getAll();
|
|
35
|
-
const allUserIds = await
|
|
30
|
+
const allUserIds = await UserRepository.getRepo().getAllIdsAsHash();
|
|
36
31
|
await this.runStandardValidationForRepository({
|
|
37
32
|
dryRun,
|
|
38
33
|
docName: 'API Key',
|
|
39
34
|
allDocs: allApiKeys,
|
|
40
35
|
shouldDelete: (apiKey) => {
|
|
41
36
|
if (!allUserIds[apiKey.userId.toString()]) {
|
|
42
|
-
|
|
37
|
+
Logger.error(`API Key with ID: ${apiKey._id.toString()} has no valid associated user.`);
|
|
43
38
|
return true;
|
|
44
39
|
}
|
|
45
40
|
return false;
|
|
46
41
|
},
|
|
47
|
-
documentValidator:
|
|
42
|
+
documentValidator: validateApiKey,
|
|
48
43
|
deletionFunction: async (docIdsToDelete) => {
|
|
49
44
|
await apiKeyRepo.deleteList(docIdsToDelete);
|
|
50
45
|
},
|
|
@@ -54,4 +49,4 @@ class ApiKeyValidator extends BaseValidator_1.default {
|
|
|
54
49
|
});
|
|
55
50
|
}
|
|
56
51
|
}
|
|
57
|
-
|
|
52
|
+
//# sourceMappingURL=ApiKeyValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ApiKeyValidator.js","sourceRoot":"./src/","sources":["validators/common/ApiKeyValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,gBAAgB,MAAM,+CAA+C,CAAC;AAC7E,OAAO,cAAc,MAAM,6CAA6C,CAAC;AACzE,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAE7C,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,UAAkB;IAC7D,KAAK,CAAC,iBAAiB,CAAC,SAAiB;QACvC,2BAA2B;QAC3B,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,EAAE,CAAC;QAC1C,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;QAC/D,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,UAAU,CAAC,UAAU,CACnB,iBAAiB,SAAS,CAAC,MAAM,CAAC,QAAQ,EAAE,kCAAkC,EAC9E,SAAS,CACV,CAAC;YACF,OAAO;QACT,CAAC;QACD,2CAA2C;QAC3C,MAAM,UAAU,GAAG,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC9C,MAAM,UAAU,GAAG,MAAM,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AC,UAAU,CAAC,YAAY,CAAC,CAAC;YAC5C,CAAC;SACF,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ApiKey, validateApiKey } from '@aneuhold/core-ts-db-lib';
|
|
2
|
+
import { ErrorUtils, Logger } from '@aneuhold/core-ts-lib';
|
|
3
|
+
import { ObjectId } from 'bson';
|
|
4
|
+
import ApiKeyRepository from '../../repositories/common/ApiKeyRepository.js';
|
|
5
|
+
import UserRepository from '../../repositories/common/UserRepository.js';
|
|
6
|
+
import IValidator from '../BaseValidator.js';
|
|
7
|
+
|
|
8
|
+
export default class ApiKeyValidator extends IValidator<ApiKey> {
|
|
9
|
+
async validateNewObject(newApiKey: ApiKey): Promise<void> {
|
|
10
|
+
// Check if the user exists
|
|
11
|
+
const userRepo = UserRepository.getRepo();
|
|
12
|
+
const userInDb = await userRepo.get({ _id: newApiKey.userId });
|
|
13
|
+
if (!userInDb) {
|
|
14
|
+
ErrorUtils.throwError(
|
|
15
|
+
`User with ID: ${newApiKey.userId.toString()} does not exist in the database.`,
|
|
16
|
+
newApiKey
|
|
17
|
+
);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
// Check if the user already has an API key
|
|
21
|
+
const apiKeyRepo = ApiKeyRepository.getRepo();
|
|
22
|
+
const apiKeyInDb = await apiKeyRepo.get({ userId: newApiKey.userId });
|
|
23
|
+
if (apiKeyInDb) {
|
|
24
|
+
ErrorUtils.throwError(
|
|
25
|
+
`User with ID: ${newApiKey.userId.toString()} already has an API key.`,
|
|
26
|
+
newApiKey
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
32
|
+
async validateUpdateObject(updatedApiKey: Partial<ApiKey>): Promise<void> {
|
|
33
|
+
// Throw, because API keys should not be updated. Only created and deleted.
|
|
34
|
+
ErrorUtils.throwError(
|
|
35
|
+
`API keys should not be updated at this time. Only created and deleted.`,
|
|
36
|
+
updatedApiKey
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async validateRepositoryInDb(dryRun: boolean): Promise<void> {
|
|
41
|
+
const apiKeyRepo = ApiKeyRepository.getRepo();
|
|
42
|
+
const allApiKeys = await apiKeyRepo.getAll();
|
|
43
|
+
const allUserIds = await UserRepository.getRepo().getAllIdsAsHash();
|
|
44
|
+
|
|
45
|
+
await this.runStandardValidationForRepository({
|
|
46
|
+
dryRun,
|
|
47
|
+
docName: 'API Key',
|
|
48
|
+
allDocs: allApiKeys,
|
|
49
|
+
shouldDelete: (apiKey: ApiKey) => {
|
|
50
|
+
if (!allUserIds[apiKey.userId.toString()]) {
|
|
51
|
+
Logger.error(
|
|
52
|
+
`API Key with ID: ${apiKey._id.toString()} has no valid associated user.`
|
|
53
|
+
);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
},
|
|
58
|
+
documentValidator: validateApiKey,
|
|
59
|
+
deletionFunction: async (docIdsToDelete: ObjectId[]) => {
|
|
60
|
+
await apiKeyRepo.deleteList(docIdsToDelete);
|
|
61
|
+
},
|
|
62
|
+
updateFunction: async (docsToUpdate: ApiKey[]) => {
|
|
63
|
+
await apiKeyRepo.updateMany(docsToUpdate);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import { User } from '@aneuhold/core-ts-db-lib';
|
|
2
|
-
import IValidator from '../BaseValidator';
|
|
2
|
+
import IValidator from '../BaseValidator.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validator for the User class.
|
|
5
|
+
*/
|
|
3
6
|
export default class UserValidator extends IValidator<User> {
|
|
4
7
|
validateNewObject(newUser: User): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Validates the object to be updated.
|
|
10
|
+
*
|
|
11
|
+
* @param userToUpdate - The user to be updated.
|
|
12
|
+
* @returns A promise that resolves when the validation is complete.
|
|
13
|
+
*/
|
|
5
14
|
validateUpdateObject(userToUpdate: Partial<User>): Promise<void>;
|
|
6
15
|
validateRepositoryInDb(dryRun: boolean): Promise<void>;
|
|
7
16
|
/**
|
|
8
17
|
* Checks if the username exists already and throws an error if it does.
|
|
18
|
+
*
|
|
19
|
+
* @param userRepo - The user repository
|
|
20
|
+
* @param userName - The username to check
|
|
21
|
+
* @throws An error if the username already exists
|
|
9
22
|
*/
|
|
10
23
|
private checkIfUserNameExists;
|
|
11
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UserValidator.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"UserValidator.d.ts","sourceRoot":"./src/","sources":["validators/common/UserValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAgB,MAAM,0BAA0B,CAAC;AAK9D,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAE7C;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,UAAU,CAAC,IAAI,CAAC;IACnD,iBAAiB,CAAC,OAAO,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAMrD;;;;;OAKG;IACG,oBAAoB,CAAC,YAAY,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA6BhE,sBAAsB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IA2B5D;;;;;;OAMG;YACW,qBAAqB;CAWpC"}
|
|
@@ -1,29 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const testsUtil_1 = require("../../tests/testsUtil");
|
|
11
|
-
class UserValidator extends BaseValidator_1.default {
|
|
1
|
+
import { User, validateUser } from '@aneuhold/core-ts-db-lib';
|
|
2
|
+
import { ErrorUtils, Logger } from '@aneuhold/core-ts-lib';
|
|
3
|
+
import UserRepository from '../../repositories/common/UserRepository.js';
|
|
4
|
+
import { TEST_USER_NAME_PREFIX } from '../../tests/globalTestVariables.js';
|
|
5
|
+
import IValidator from '../BaseValidator.js';
|
|
6
|
+
/**
|
|
7
|
+
* Validator for the User class.
|
|
8
|
+
*/
|
|
9
|
+
export default class UserValidator extends IValidator {
|
|
12
10
|
async validateNewObject(newUser) {
|
|
13
11
|
// Check if the username already exists
|
|
14
|
-
const userRepo =
|
|
12
|
+
const userRepo = UserRepository.getRepo();
|
|
15
13
|
await this.checkIfUserNameExists(userRepo, newUser.userName);
|
|
16
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Validates the object to be updated.
|
|
17
|
+
*
|
|
18
|
+
* @param userToUpdate - The user to be updated.
|
|
19
|
+
* @returns A promise that resolves when the validation is complete.
|
|
20
|
+
*/
|
|
17
21
|
async validateUpdateObject(userToUpdate) {
|
|
18
22
|
// Check if an id is defined
|
|
19
23
|
if (!userToUpdate._id) {
|
|
20
|
-
|
|
24
|
+
ErrorUtils.throwError(`No _id defined for ${User.name} to update.`, userToUpdate);
|
|
21
25
|
}
|
|
22
26
|
// Check to see if the user exists
|
|
23
|
-
const userRepo =
|
|
27
|
+
const userRepo = UserRepository.getRepo();
|
|
24
28
|
const userInDb = await userRepo.get({ _id: userToUpdate._id });
|
|
25
29
|
if (!userInDb) {
|
|
26
|
-
|
|
30
|
+
ErrorUtils.throwError(`${User.name} with ID: ${userToUpdate._id?.toString()} does not exist in the database.`, userToUpdate);
|
|
27
31
|
return;
|
|
28
32
|
}
|
|
29
33
|
// Check if the username is being updated and, if it is, if it already
|
|
@@ -33,20 +37,20 @@ class UserValidator extends BaseValidator_1.default {
|
|
|
33
37
|
}
|
|
34
38
|
}
|
|
35
39
|
async validateRepositoryInDb(dryRun) {
|
|
36
|
-
const userRepo =
|
|
40
|
+
const userRepo = UserRepository.getRepo();
|
|
37
41
|
const allUsers = await userRepo.getAll();
|
|
38
42
|
await this.runStandardValidationForRepository({
|
|
39
43
|
dryRun,
|
|
40
44
|
docName: 'User',
|
|
41
45
|
allDocs: allUsers,
|
|
42
46
|
shouldDelete: (user) => {
|
|
43
|
-
if (user.userName.startsWith(
|
|
44
|
-
|
|
47
|
+
if (user.userName.startsWith(TEST_USER_NAME_PREFIX)) {
|
|
48
|
+
Logger.error(`User with ID: ${user._id.toString()} is a test user and should be deleted`);
|
|
45
49
|
return true;
|
|
46
50
|
}
|
|
47
51
|
return false;
|
|
48
52
|
},
|
|
49
|
-
documentValidator:
|
|
53
|
+
documentValidator: validateUser,
|
|
50
54
|
deletionFunction: async (docIdsToDelete) => {
|
|
51
55
|
await userRepo.deleteList(docIdsToDelete);
|
|
52
56
|
},
|
|
@@ -57,14 +61,18 @@ class UserValidator extends BaseValidator_1.default {
|
|
|
57
61
|
}
|
|
58
62
|
/**
|
|
59
63
|
* Checks if the username exists already and throws an error if it does.
|
|
64
|
+
*
|
|
65
|
+
* @param userRepo - The user repository
|
|
66
|
+
* @param userName - The username to check
|
|
67
|
+
* @throws An error if the username already exists
|
|
60
68
|
*/
|
|
61
69
|
async checkIfUserNameExists(userRepo, userName) {
|
|
62
70
|
const userNameSearchResult = await userRepo.get({
|
|
63
71
|
userName
|
|
64
72
|
});
|
|
65
73
|
if (userNameSearchResult) {
|
|
66
|
-
|
|
74
|
+
ErrorUtils.throwError('Username already exists', { userName });
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
77
|
}
|
|
70
|
-
|
|
78
|
+
//# sourceMappingURL=UserValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserValidator.js","sourceRoot":"./src/","sources":["validators/common/UserValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE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ib';
|
|
3
|
+
import { ObjectId } from 'bson';
|
|
4
|
+
import UserRepository from '../../repositories/common/UserRepository.js';
|
|
5
|
+
import { TEST_USER_NAME_PREFIX } from '../../tests/globalTestVariables.js';
|
|
6
|
+
import IValidator from '../BaseValidator.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Validator for the User class.
|
|
10
|
+
*/
|
|
11
|
+
export default class UserValidator extends IValidator<User> {
|
|
12
|
+
async validateNewObject(newUser: User): Promise<void> {
|
|
13
|
+
// Check if the username already exists
|
|
14
|
+
const userRepo = UserRepository.getRepo();
|
|
15
|
+
await this.checkIfUserNameExists(userRepo, newUser.userName);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Validates the object to be updated.
|
|
20
|
+
*
|
|
21
|
+
* @param userToUpdate - The user to be updated.
|
|
22
|
+
* @returns A promise that resolves when the validation is complete.
|
|
23
|
+
*/
|
|
24
|
+
async validateUpdateObject(userToUpdate: Partial<User>): Promise<void> {
|
|
25
|
+
// Check if an id is defined
|
|
26
|
+
if (!userToUpdate._id) {
|
|
27
|
+
ErrorUtils.throwError(
|
|
28
|
+
`No _id defined for ${User.name} to update.`,
|
|
29
|
+
userToUpdate
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check to see if the user exists
|
|
34
|
+
const userRepo = UserRepository.getRepo();
|
|
35
|
+
const userInDb = await userRepo.get({ _id: userToUpdate._id });
|
|
36
|
+
if (!userInDb) {
|
|
37
|
+
ErrorUtils.throwError(
|
|
38
|
+
`${
|
|
39
|
+
User.name
|
|
40
|
+
} with ID: ${userToUpdate._id?.toString()} does not exist in the database.`,
|
|
41
|
+
userToUpdate
|
|
42
|
+
);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Check if the username is being updated and, if it is, if it already
|
|
47
|
+
// exists
|
|
48
|
+
if (userToUpdate.userName && userInDb.userName !== userToUpdate.userName) {
|
|
49
|
+
await this.checkIfUserNameExists(userRepo, userToUpdate.userName);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async validateRepositoryInDb(dryRun: boolean): Promise<void> {
|
|
54
|
+
const userRepo = UserRepository.getRepo();
|
|
55
|
+
const allUsers = await userRepo.getAll();
|
|
56
|
+
|
|
57
|
+
await this.runStandardValidationForRepository({
|
|
58
|
+
dryRun,
|
|
59
|
+
docName: 'User',
|
|
60
|
+
allDocs: allUsers,
|
|
61
|
+
shouldDelete: (user: User) => {
|
|
62
|
+
if (user.userName.startsWith(TEST_USER_NAME_PREFIX)) {
|
|
63
|
+
Logger.error(
|
|
64
|
+
`User with ID: ${user._id.toString()} is a test user and should be deleted`
|
|
65
|
+
);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
},
|
|
70
|
+
documentValidator: validateUser,
|
|
71
|
+
deletionFunction: async (docIdsToDelete: ObjectId[]) => {
|
|
72
|
+
await userRepo.deleteList(docIdsToDelete);
|
|
73
|
+
},
|
|
74
|
+
updateFunction: async (docsToUpdate: User[]) => {
|
|
75
|
+
await userRepo.updateMany(docsToUpdate);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Checks if the username exists already and throws an error if it does.
|
|
82
|
+
*
|
|
83
|
+
* @param userRepo - The user repository
|
|
84
|
+
* @param userName - The username to check
|
|
85
|
+
* @throws An error if the username already exists
|
|
86
|
+
*/
|
|
87
|
+
private async checkIfUserNameExists(
|
|
88
|
+
userRepo: UserRepository,
|
|
89
|
+
userName: string
|
|
90
|
+
) {
|
|
91
|
+
const userNameSearchResult = await userRepo.get({
|
|
92
|
+
userName
|
|
93
|
+
});
|
|
94
|
+
if (userNameSearchResult) {
|
|
95
|
+
ErrorUtils.throwError('Username already exists', { userName });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NonogramKatanaItem } from '@aneuhold/core-ts-db-lib';
|
|
2
|
-
import IValidator from '../BaseValidator';
|
|
2
|
+
import IValidator from '../BaseValidator.js';
|
|
3
3
|
export default class DashboardNonogramKatanaItemValidator extends IValidator<NonogramKatanaItem> {
|
|
4
4
|
validateNewObject(newItem: NonogramKatanaItem): Promise<void>;
|
|
5
5
|
validateUpdateObject(updatedItem: Partial<NonogramKatanaItem>): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NonogramKatanaItemValidator.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"NonogramKatanaItemValidator.d.ts","sourceRoot":"./src/","sources":["validators/dashboard/NonogramKatanaItemValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAKlC,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAE7C,MAAM,CAAC,OAAO,OAAO,oCAAqC,SAAQ,UAAU,CAAC,kBAAkB,CAAC;IACxF,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;IAwB7D,oBAAoB,CACxB,WAAW,EAAE,OAAO,CAAC,kBAAkB,CAAC,GACvC,OAAO,CAAC,IAAI,CAAC;IAUV,sBAAsB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CA8B7D"}
|
|
@@ -1,54 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const core_ts_lib_1 = require("@aneuhold/core-ts-lib");
|
|
8
|
-
const BaseValidator_1 = __importDefault(require("../BaseValidator"));
|
|
9
|
-
const UserRepository_1 = __importDefault(require("../../repositories/common/UserRepository"));
|
|
10
|
-
const DashboardNonogramKatanaItemRepository_1 = __importDefault(require("../../repositories/dashboard/DashboardNonogramKatanaItemRepository"));
|
|
11
|
-
class DashboardNonogramKatanaItemValidator extends BaseValidator_1.default {
|
|
1
|
+
import { validateNonogramKatanaItem } from '@aneuhold/core-ts-db-lib';
|
|
2
|
+
import { ErrorUtils, Logger } from '@aneuhold/core-ts-lib';
|
|
3
|
+
import UserRepository from '../../repositories/common/UserRepository.js';
|
|
4
|
+
import DashboardNonogramKatanaItemRepository from '../../repositories/dashboard/DashboardNonogramKatanaItemRepository.js';
|
|
5
|
+
import IValidator from '../BaseValidator.js';
|
|
6
|
+
export default class DashboardNonogramKatanaItemValidator extends IValidator {
|
|
12
7
|
async validateNewObject(newItem) {
|
|
13
8
|
// Check if the item already exists for the user
|
|
14
|
-
const itemRepo =
|
|
9
|
+
const itemRepo = DashboardNonogramKatanaItemRepository.getRepo();
|
|
15
10
|
const existingItem = await itemRepo.get({
|
|
16
11
|
userId: newItem.userId,
|
|
17
12
|
itemName: newItem.itemName
|
|
18
13
|
});
|
|
19
14
|
if (existingItem) {
|
|
20
|
-
|
|
15
|
+
ErrorUtils.throwError(`Nonogram Katana item already exists for user: ${newItem.userId.toString()}`, newItem);
|
|
21
16
|
}
|
|
22
|
-
const userRepo =
|
|
17
|
+
const userRepo = UserRepository.getRepo();
|
|
23
18
|
const user = await userRepo.get({ _id: newItem.userId });
|
|
24
19
|
if (!user) {
|
|
25
|
-
|
|
20
|
+
ErrorUtils.throwError(`User does not exist: ${newItem.userId.toString()}`, newItem);
|
|
26
21
|
}
|
|
27
22
|
}
|
|
28
23
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
29
24
|
async validateUpdateObject(updatedItem) {
|
|
30
25
|
// Check if an id is defined
|
|
31
26
|
if (!updatedItem._id) {
|
|
32
|
-
|
|
27
|
+
ErrorUtils.throwError(`No _id defined for NonogramKatanaItem update.`, updatedItem);
|
|
33
28
|
}
|
|
34
29
|
}
|
|
35
30
|
async validateRepositoryInDb(dryRun) {
|
|
36
|
-
const itemRepo =
|
|
31
|
+
const itemRepo = DashboardNonogramKatanaItemRepository.getRepo();
|
|
37
32
|
const allItems = await itemRepo.getAll();
|
|
38
|
-
const allUserIds = await
|
|
33
|
+
const allUserIds = await UserRepository.getRepo().getAllIdsAsHash();
|
|
39
34
|
await this.runStandardValidationForRepository({
|
|
40
35
|
dryRun,
|
|
41
36
|
docName: 'Nonogram Katana Item',
|
|
42
37
|
allDocs: allItems,
|
|
43
38
|
shouldDelete: (item) => {
|
|
44
39
|
if (!allUserIds[item.userId.toString()]) {
|
|
45
|
-
|
|
40
|
+
Logger.error(`Nonogram Katana Item with ID: ${item._id.toString()} has no valid associated user.`);
|
|
46
41
|
return true;
|
|
47
42
|
}
|
|
48
43
|
return false;
|
|
49
44
|
},
|
|
50
45
|
documentValidator: (item) => {
|
|
51
|
-
const { updatedDoc, errors } =
|
|
46
|
+
const { updatedDoc, errors } = validateNonogramKatanaItem(item);
|
|
52
47
|
return { updatedDoc, errors };
|
|
53
48
|
},
|
|
54
49
|
deletionFunction: async (docIdsToDelete) => {
|
|
@@ -60,4 +55,4 @@ class DashboardNonogramKatanaItemValidator extends BaseValidator_1.default {
|
|
|
60
55
|
});
|
|
61
56
|
}
|
|
62
57
|
}
|
|
63
|
-
|
|
58
|
+
//# sourceMappingURL=NonogramKatanaItemValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NonogramKatanaItemValidator.js","sourceRoot":"./src/","sources":["validators/dashboard/NonogramKatanaItemValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,0BAA0B,EAC3B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,cAAc,MAAM,6CAA6C,CAAC;AACzE,OAAO,qCAAqC,MAAM,uEAAuE,CAAC;AAC1H,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAE7C,MAAM,CAAC,OAAO,OAAO,oCAAqC,SAAQ,UAA8B;IAC9F,KAAK,CAAC,iBAAiB,CAAC,OAA2B;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