Skip to content

Commit

Permalink
create app to create user
Browse files Browse the repository at this point in the history
  • Loading branch information
kevynsax committed Jun 28, 2020
1 parent f59d57b commit e072925
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/model/user/UserApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import {IUserRepo} from "src/model/user/UserRepo";
import {Geolocation, User} from "src/model/user/types";

export interface IUserApp {
createUser: (ipAddress: string) => Promise<string>
}

export class UserApp implements IUserApp {

constructor(private repo: IUserRepo){}

public createUser = (ipAddress: string) => {
const user: User = { ipAddress, geolocation: {} as Geolocation };

return this.repo.create(user)
.then(x => x._id);
};
}
6 changes: 6 additions & 0 deletions src/model/user/UserRepo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {User} from "src/model/user/types";
import {Model, Document} from "mongoose";

export type UserModel = User & Document;

export type IUserRepo = Model<UserModel>;
21 changes: 18 additions & 3 deletions src/model/user/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
export interface User {

ipAddress: string;
geolocation: Geolocation;
}

// geolocation: {
export interface Geolocation {
continent_code: string,
continent_name: string,
country_code: string,
country_name: string,
region_code: string,
region_name: string,
city: string,
zip: string,
latitude: number,
longitude: number,
}

//
// export interface Geolocation {
// continent_code: "SA",
// continent_name: "South America",
// country_code: "BR",
Expand All @@ -13,4 +28,4 @@ export interface User {
// zip: "71010-001",
// latitude: -15.819999694824219,
// longitude: -47.98400115966797,
// }
// }
27 changes: 27 additions & 0 deletions src/tests/model/user/UserApp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {IUserRepo, UserModel} from "src/model/user/UserRepo";
import {UserApp} from "src/model/user/UserApp";

describe('Insert User', () => {

test('Happy path', async () => {

// given
const fakeIp = "fake ip";
const fakeId = "this is a fakeId Number" ;

const create = jest.fn()
.mockImplementationOnce(() =>
new Promise<UserModel>(resolve => resolve({_id: fakeId} as UserModel)));

const repo = {
create
} as unknown as IUserRepo;
const target = new UserApp(repo);

// when
const result = await target.createUser(fakeIp);

// then
expect(result).toBe(fakeId);
});
});

0 comments on commit e072925

Please sign in to comment.