Skip to content

Commit

Permalink
Merge pull request #79 from iotaledger/78-product-classification
Browse files Browse the repository at this point in the history
78 product classification
  • Loading branch information
Dominic Zettl committed Apr 8, 2021
2 parents 870e2f8 + 818c534 commit 707e51c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 65 deletions.
18 changes: 2 additions & 16 deletions api/src/database/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,19 @@ export const updateUser = async (user: UserPersistence): Promise<UpdateWriteOpRe
_id: user.userId
};

const {
firstName,
lastName,
username,
organization,
subscribedChannelIds,
description,
classification,
organizationUrl,
location,
verifiableCredentials
} = user;
const { username, organization, subscribedChannelIds, description, classification, details, verifiableCredentials } = user;

if (verifiableCredentials?.some((vc) => vc?.id !== user.userId)) {
throw new Error('the passed verifiable credentials does not concur with the user!');
}

const updateObject = MongoDbService.getPlainObject({
firstName,
lastName,
description,
username: username || undefined, // username must not be ''
classification: classification || undefined, // username must not be ''
organization,
subscribedChannelIds,
organizationUrl,
location,
details,
verifiableCredentials
});

Expand Down
3 changes: 2 additions & 1 deletion api/src/middlewares/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import { ValidationError } from 'express-json-validator-middleware';
* @param {NextFunction} _next Next function
*/
export const errorMiddleware = (err: Error, _req: Request, res: Response, _next: NextFunction): void => {
console.error(err);
if (err instanceof ValidationError) {
console.error(JSON.stringify(err));
res.status(StatusCodes.BAD_REQUEST);
res.send({ error: 'No valid body provided!' });
return;
}
console.error(err);
res.status(StatusCodes.INTERNAL_SERVER_ERROR);
res.send({ error: err.message });
};
9 changes: 3 additions & 6 deletions api/src/models/schemas/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ export const VerificationSchema = Type.Object({
const UserWithoutIdFields = {
username: Type.String({ minLength: 3 }),
classification: Type.String({ minLength: 3 }),
firstName: Type.Optional(Type.Union([Type.String(), Type.Null()])),
lastName: Type.Optional(Type.Union([Type.String(), Type.Null()])),
organization: Type.Optional(Type.Union([Type.String({ minLength: 2 }), Type.Null()])),
subscribedChannelIds: Type.Optional(Type.Union([Type.Array(Type.String()), Type.Null()])),
registrationDate: Type.Optional(Type.Union([Type.String(), Type.Null()])),
verification: Type.Optional(Type.Union([VerificationSchema, Type.Null()])),
description: Type.Optional(Type.Union([Type.String(), Type.Null()])),
location: Type.Optional(Type.Union([LocationSchema, Type.Null()])),
organizationUrl: Type.Optional(Type.Union([Type.String(), Type.Null()])),
organization: Type.Optional(Type.Union([Type.String({ minLength: 2 }), Type.Null()])),
verifiableCredentials: Type.Optional(Type.Union([Type.Array(VerifiableCredentialSchema), Type.Null()])),
role: Type.Optional(Type.Union([Type.String(), Type.Null()]))
role: Type.Optional(Type.Union([Type.String(), Type.Null()])),
details: Type.Optional(Type.Union([Type.Any(), Type.Null()]))
};

export const UserWithoutIdSchema = Type.Object({
Expand Down
9 changes: 6 additions & 3 deletions api/src/models/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ export type Verification = Static<typeof VerificationSchema>;

export const enum UserClassification {
'human' = 'human',
'api' = 'api',
'organization' = 'organization',
'device' = 'device',
'api' = 'api'
'product' = 'product',
'unknown' = 'unknown'
}

export const enum UserRoles {
Expand All @@ -23,7 +26,7 @@ export interface UserSearch {
subscribedChannelIds?: string[];
verified?: boolean;
registrationDate?: Date;
classification?: UserClassification;
classification?: UserClassification | string;
limit?: number;
index?: number;
}
Expand All @@ -34,7 +37,7 @@ export interface UserPersistence extends OmittedUser {
role?: UserRoles;
verification?: VerificationPersistence;
registrationDate?: Date;
classification: UserClassification;
classification: UserClassification | string;
}

export interface VerificationPersistence {
Expand Down
12 changes: 4 additions & 8 deletions api/src/routes/user/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ describe('test GET user', () => {
username: 'first-user',
classification: UserClassification.human,
subscribedChannelIds: [],
firstName: 'Tom',
lastName: 'Tomson',
details: { firstName: 'Tom', lastName: 'Tomson' },
description: null,
registrationDate: date,
organization: 'IOTA'
Expand All @@ -108,8 +107,7 @@ describe('test GET user', () => {
username: 'first-user',
classification: 'human',
subscribedChannelIds: [],
firstName: 'Tom',
lastName: 'Tomson',
details: { firstName: 'Tom', lastName: 'Tomson' },
description: null,
registrationDate: getDateStringFromDate(date),
organization: 'IOTA'
Expand Down Expand Up @@ -141,8 +139,7 @@ describe('test POST user', () => {
username: 'first-user',
classification: UserClassification.human,
subscribedChannelIds: [],
firstName: 'Tom',
lastName: 'Tomson',
details: { firstName: 'Tom', lastName: 'Sonson' },
description: null,
registrationDate: '2021-02-12T14:58:05+01:00',
organization: 'IOTA'
Expand Down Expand Up @@ -225,8 +222,7 @@ describe('test PUT user', () => {
username: 'first-user',
classification: UserClassification.human,
subscribedChannelIds: [],
firstName: 'Tom',
lastName: 'Tomson',
details: { firstName: 'Tom', lastName: 'Sonson' },
description: null,
registrationDate: '2021-02-12T14:58:05+01:00',
organization: 'IOTA'
Expand Down
34 changes: 5 additions & 29 deletions api/src/services/user-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
User,
UserClassification,
UserPersistence,
UserRoles,
UserSearch,
Expand Down Expand Up @@ -98,8 +97,6 @@ export class UserService {
throw new Error('Error when parsing the body: userId must be provided!');
}
const {
firstName,
lastName,
publicKey,
subscribedChannelIds,
userId,
Expand All @@ -109,37 +106,22 @@ export class UserService {
registrationDate,
classification,
description,
organizationUrl,
location,
details,
verifiableCredentials,
role
} = user;

if (
classification &&
classification !== UserClassification.human &&
classification !== UserClassification.device &&
classification !== UserClassification.api
) {
throw new Error(
`No valid classification provided, it must be ${UserClassification.human}, ${UserClassification.device} or ${UserClassification.api}!`
);
}

const userPersistence: UserPersistence = {
userId,
publicKey,
username,
classification: classification as UserClassification,
classification,
subscribedChannelIds,
firstName,
lastName,
description,
organization,
registrationDate: registrationDate && getDateFromString(registrationDate),
verification: this.getVerificationPersistence(verification),
organizationUrl,
location,
details,
verifiableCredentials,
role: role && (role as UserRoles)
};
Expand All @@ -154,19 +136,16 @@ export class UserService {
}

const {
firstName,
username,
publicKey,
userId,
subscribedChannelIds,
organization,
lastName,
registrationDate,
verification,
classification,
description,
organizationUrl,
location,
details,
verifiableCredentials,
role
} = userPersistence;
Expand All @@ -177,14 +156,11 @@ export class UserService {
username,
classification,
subscribedChannelIds,
firstName,
lastName,
description,
registrationDate: getDateStringFromDate(registrationDate),
verification: this.getVerificationObject(verification),
organization,
organizationUrl,
location,
details,
verifiableCredentials,
role: role && (role as UserRoles)
};
Expand Down
3 changes: 1 addition & 2 deletions api/src/test/mocks/identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ export const UserIdentityMock: IdentityJsonUpdate & { userData: User } = {
publicKey: '8WaGsr277JQaqV9fxHmFNGC9haApFbBfdnytmq5gq4vm',
username: 'first-user',
classification: 'human',
firstName: 'Tom',
lastName: 'Sonson',
details: { firstName: 'Tom', lastName: 'Sonson' },
description: 'Just a user',
registrationDate: '2021-03-16T15:18:49+01:00',
verification: {
Expand Down

0 comments on commit 707e51c

Please sign in to comment.