Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@
"passport-slack": "0.0.7",
"passport-slack-oauth2": "^1.0.2",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.29"
"typeorm": "0.2.29",
"uuid": "^8.3.2"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
Expand Down
10 changes: 2 additions & 8 deletions src/OAuth/Application/CreateGoogleUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { inject } from 'inversify';
import TYPES from '../../constant/types';
import { InputService } from '../../InputService';
import { Logger } from '../../Logger';
//import { InvalidUserCreate } from '../Domain/InvalidUserCreate';
import { User } from '../Domain/User';
import { UserRepository } from '../Domain/UserRepository';
import { ValidUser } from '../Domain/ValidUser';

export class CreateGoogleUser implements InputService {
private readonly logger: Logger;
Expand All @@ -18,14 +18,8 @@ export class CreateGoogleUser implements InputService {
this.logger = logger;
}

//@ts-ignore
async execute(googleUser: ValidUser): any {
async execute(googleUser: ValidUser): Promise<any> {
try {
//const repoPromise = this.repo.findUserByEmail(googleUser.email);
//const user = await repoPromise.then(userdata => userdata);
/*if (user) {
throw new InvalidUserCreate(`El usuario ${user.email} ya se encuentra registrado`);
}*/
this.logger.log(`Intentando almacenar ${JSON.stringify(googleUser)}`);
await this.repo.createUser(new User(googleUser));
return { success: 'true' };
Expand Down
30 changes: 30 additions & 0 deletions src/OAuth/Application/DeleteGithubUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { inject } from 'inversify';
import TYPES from '../../constant/types';
import { InputService } from '../../InputService';
import { Logger } from '../../Logger';
import { UserRepository } from '../Domain/UserRepository';

export class DeleteGithubUser implements InputService {
private readonly logger: Logger;
private readonly repo: UserRepository;

constructor(
@inject(TYPES.UserRepository) repo: UserRepository,
@inject(TYPES.Logger) logger: Logger,
) {
this.repo = repo;
this.logger = logger;
}

//@ts-ignore
async execute(id: string): Promise<any> {
try {
this.logger.log(`Intentando borrar ${JSON.stringify(id)}`);
await this.repo.deleteUser(id);
return { success: 'true' };
} catch (err) {
this.logger.error(err);
throw err;
}
}
}
30 changes: 30 additions & 0 deletions src/OAuth/Application/DeleteGoogleUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { inject } from 'inversify';
import TYPES from '../../constant/types';
import { InputService } from '../../InputService';
import { Logger } from '../../Logger';
import { UserRepository } from '../Domain/UserRepository';

export class DeleteGoogleUser implements InputService {
private readonly logger: Logger;
private readonly repo: UserRepository;

constructor(
@inject(TYPES.UserRepository) repo: UserRepository,
@inject(TYPES.Logger) logger: Logger,
) {
this.repo = repo;
this.logger = logger;
}

//@ts-ignore
async execute(id: string): Promise<any> {
try {
this.logger.log(`Intentando borrar ${JSON.stringify(id)}`);
await this.repo.deleteUser(id);
return { success: 'true' };
} catch (err) {
this.logger.error(err);
throw err;
}
}
}
30 changes: 30 additions & 0 deletions src/OAuth/Application/DeleteSlackUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { inject } from 'inversify';
import TYPES from '../../constant/types';
import { InputService } from '../../InputService';
import { Logger } from '../../Logger';
import { UserRepository } from '../Domain/UserRepository';

export class DeleteSlackUser implements InputService {
private readonly logger: Logger;
private readonly repo: UserRepository;

constructor(
@inject(TYPES.UserRepository) repo: UserRepository,
@inject(TYPES.Logger) logger: Logger,
) {
this.repo = repo;
this.logger = logger;
}

//@ts-ignore
async execute(id: string): Promise<any> {
try {
this.logger.log(`Intentando borrar ${JSON.stringify(id)}`);
await this.repo.deleteUser(id);
return { success: 'true' };
} catch (err) {
this.logger.error(err);
throw err;
}
}
}
13 changes: 13 additions & 0 deletions src/OAuth/Domain/UUID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { v4 as uuidv4 } from 'uuid';

export class UUID {
readonly value: string;

constructor() {
this.value = uuidv4();
}

equals(id: UUID) {
return this.value === id.value;
}
}
17 changes: 12 additions & 5 deletions src/OAuth/Domain/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Email } from './Email';
import { InvalidUserError } from './InvalidUserError';
import { ValidUser } from './ValidUser';
import { UserName } from './UsesrName';
import { UUID } from './UUID';

export class User {
_id: Id;
_id: UUID;
_idFromProvider: Id;
_displayName: Name;
_username: UserName;
_image: Image;
Expand All @@ -18,7 +20,8 @@ export class User {
constructor(user: ValidUser) {
// TODO: Refactor it should be able to handle multiple errors
try {
this.id = user.id;
this._id = new UUID();
this.idFromProvider = user.idFromProvider;
this.displayName = user.displayName;
this.username = user.username;
this.image = user.image;
Expand All @@ -37,8 +40,12 @@ export class User {
return this._id.value;
}

set id(id: string) {
this._id = new Id(id);
get idFromProvider(): string {
return this._idFromProvider.value;
}

set idFromProvider(idFromProvider: string) {
this._idFromProvider = new Id(idFromProvider);
}

get displayName(): string {
Expand Down Expand Up @@ -84,7 +91,7 @@ export class User {
equals(user: ValidUser): boolean {
const comparator = new User(user);
return (
this._id.equals(comparator._id) &&
this._idFromProvider.equals(comparator._idFromProvider) &&
this._displayName.equals(comparator._displayName) &&
this._image.equals(comparator._image) &&
this._username.equals(comparator._username) &&
Expand Down
8 changes: 8 additions & 0 deletions src/OAuth/Domain/UserNotExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UserNotExists extends Error {
constructor(message: string) {
super(`UserNotExists: ${message}`);
Object.setPrototypeOf(this, UserNotExists.prototype);
}
}

export { UserNotExists };
2 changes: 1 addition & 1 deletion src/OAuth/Domain/ValidUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface ValidUser {
readonly id: string;
readonly idFromProvider: string;
readonly displayName: string;
readonly username: string;
readonly image: string;
Expand Down
17 changes: 15 additions & 2 deletions src/OAuth/Infraestructure/GithubController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { UserRepository } from './../Domain/UserRepository';
import { controller, httpGet } from 'inversify-express-utils';
import { controller, httpGet, response, requestParam } from 'inversify-express-utils';
import { Request, Response, NextFunction } from 'express';
import { OauthMiddleware } from './OauthMiddleware';
import { Logger } from '../../Logger';
import { inject } from 'inversify';
import TYPES from '../../constant/types';
import { CreateGithubUser } from '../Application/CreateGithubUser';
import PROVIDER from '../../constant/providers';
import { DeleteGithubUser } from '../Application/DeleteGithubUser';
const OAUTH_PROVIDER = 'github';
const OAUTH_CONFIG = {
scope: ['user:email'],
Expand Down Expand Up @@ -46,12 +47,24 @@ export class GithubController {
const displayName = user.displayName ? user.displayName : user.username;

return createGithubUser.execute({
id: user.id,
idFromProvider: user.id,
displayName: displayName,
username: user.username,
image: user.photos[0].value,
email: email,
provider: PROVIDER.GITHUB,
});
}

@httpGet('/delete/:id')
public async delete(@response() response: Response, @requestParam('id') idParam: string) {
const deleteGithubUser = new DeleteGithubUser(this.repo, this.logger);

try {
return await deleteGithubUser.execute(idParam);
} catch (error) {
this.logger.error(error);
response.sendStatus(404);
}
}
}
17 changes: 15 additions & 2 deletions src/OAuth/Infraestructure/GoogleController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { controller, httpGet } from 'inversify-express-utils';
import { controller, httpGet, response, requestParam } from 'inversify-express-utils';
import { Request, Response, NextFunction } from 'express';
import { inject } from 'inversify';
import TYPES from '../../constant/types';
Expand All @@ -7,6 +7,7 @@ import { CreateGoogleUser } from '../Application/CreateGoogleUser';
import { OauthMiddleware } from './OauthMiddleware';
import { UserRepository } from '../Domain/UserRepository';
import PROVIDER from '../../constant/providers';
import { DeleteGoogleUser } from '../Application/DeleteGoogleUser';

const OAUTH_PROVIDER = 'google';
const OAUTH_CONFIG = { scope: ['profile', 'email'] };
Expand Down Expand Up @@ -44,7 +45,7 @@ export class GoogleController {
const createGoogleUser = new CreateGoogleUser(this.repo, this.logger);
try {
return await createGoogleUser.execute({
id: user._json.sub,
idFromProvider: user._json.sub,
displayName: user._json.name,
username: user._json.email,
image: user._json.picture,
Expand All @@ -56,4 +57,16 @@ export class GoogleController {
response.sendStatus(500);
}
}

@httpGet('/delete/:id')
public async delete(@response() response: Response, @requestParam('id') idParam: string) {
const deleteGoogleUser = new DeleteGoogleUser(this.repo, this.logger);

try {
return await deleteGoogleUser.execute(idParam);
} catch (error) {
this.logger.error(error);
response.sendStatus(404);
}
}
}
Loading