Skip to content

Commit

Permalink
Merge pull request #6 from Duna-System/fix/quota-organization
Browse files Browse the repository at this point in the history
fix: insert service on usersProject
  • Loading branch information
Marcus-D-Forte authored Dec 6, 2023
2 parents e6f8c62 + d2a0d60 commit ad7dd20
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build/usersProject/UserService.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ export declare class UserService {
protected configure(): void;
get(user_id?: string, email?: string): Promise<IUsers>;
update(user: IUsers): Promise<IUsers>;
insert(user: IUsers): Promise<import("mongoose").Document<unknown, {}, IUsers> & IUsers & Required<{
_id: string;
}>>;
}
21 changes: 21 additions & 0 deletions build/usersProject/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,26 @@ class UserService {
return user_record.toJSON();
});
}
insert(user) {
return __awaiter(this, void 0, void 0, function* () {
try {
const existingUser = yield validationUserModel_1.UserModel.findOne({ email: user.email });
if (existingUser) {
const err = duna_web_platform_error_defs_1.ErrorMessages.ResourceExists;
throw err;
}
const newUser = new validationUserModel_1.UserModel(user);
const savedUser = yield newUser.save();
return savedUser;
}
catch (error) {
if (error !== duna_web_platform_error_defs_1.ErrorMessages.ResourceExists) {
error = duna_web_platform_error_defs_1.ErrorMessages.InternalServerError;
error.Details = 'Possibly wrong data schema.';
}
throw error;
}
});
}
}
exports.UserService = UserService;
24 changes: 23 additions & 1 deletion src/usersProject/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorMessages } from 'duna-web-platform-error-defs'
import { ErrorMessages, IError } from 'duna-web-platform-error-defs'
import { checkConnectionStatus } from '../connection'
import { IUsers } from '../interfaces'
import { UserModel } from '../users/validationUserModel'
Expand Down Expand Up @@ -44,4 +44,26 @@ export class UserService {

return user_record.toJSON<IUsers>()
}

public async insert(user: IUsers) {
try {
const existingUser = await UserModel.findOne({ email: user.email })

if (existingUser) {
const err: IError = ErrorMessages.ResourceExists
throw err
}

const newUser = new UserModel(user)
const savedUser = await newUser.save()

return savedUser
} catch (error) {
if (error !== ErrorMessages.ResourceExists) {
error = ErrorMessages.InternalServerError
;(error as IError).Details = 'Possibly wrong data schema.'
}
throw error
}
}
}

0 comments on commit ad7dd20

Please sign in to comment.