Skip to content

Commit

Permalink
Get ip address on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
kevynsax committed Jul 1, 2020
1 parent 18ebdcb commit 8f004ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
15 changes: 8 additions & 7 deletions src/tests/user/UserController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import {ResumeServer} from "src/ResumeServer";
import {httpStatusCode} from "src/constants";
import {UserController} from "src/user/UserController";
import {IUserApp} from "src/user/UserApp";
import {CreateUserViewModel, UserViewModel} from "src/user/userViewModels";
import {UserViewModel} from "src/user/userViewModels";
import {fakeUser} from "src/tests/user/userTestsConstants";

export const fakeRequestUser: CreateUserViewModel = {
ipAddress: fakeUser.ipAddress
};


export const fakeReturnUser: UserViewModel = {
id: "abacate",
Expand Down Expand Up @@ -39,14 +37,18 @@ describe('User Controller', () => {

test('Insert User', async () => {
// given
const req = {body: fakeRequestUser} as Request;
const req = {
connection: {
remoteAddress: fakeUser.ipAddress
}
} as Request;

// when
const result = await target.insertUser(req, fakeResponse);

// then
expect(result).toEqual(fakeReturnUser);
expect(fakeApp.createUser).toBeCalledWith(fakeRequestUser.ipAddress);
expect(fakeApp.createUser).toBeCalledWith(fakeUser.ipAddress);
});

test('Insert User - Test Endpoint', async () => {
Expand All @@ -55,7 +57,6 @@ describe('User Controller', () => {

const response = await agent
.post(`/api/user`)
.send(fakeRequestUser)
.expect(httpStatusCode.success)
.then(x => x.text);

Expand Down
6 changes: 3 additions & 3 deletions src/user/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Endpoint, injectHelpers} from "src/middleware/injectHelpers";
import {ClassMiddleware, Controller, Post} from "@overnightjs/core";
import {IUserApp} from "src/user/UserApp";
import {CreateUserViewModel, UserViewModel} from "src/user/userViewModels";
import {UserViewModel} from "src/user/userViewModels";

@Controller("api/user")
@ClassMiddleware(injectHelpers)
Expand All @@ -10,8 +10,8 @@ export class UserController {
constructor(private app: IUserApp){}

@Post("")
public insertUser: Endpoint = async (req, res): Promise<void> =>{
const {ipAddress} = req.body as CreateUserViewModel;
public insertUser: Endpoint = async (req, res): Promise<void> => {
const ipAddress = req.connection?.remoteAddress || "";

const result = this.app.createUser(ipAddress)
.then(id => ({id} as UserViewModel));
Expand Down
24 changes: 12 additions & 12 deletions src/user/UserRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ export type UserModel = User & Document;
export type IUserRepo = Model<UserModel>;

const schema = new Schema<UserModel>({
ipAddress: {type: String, required: true},
geolocation: {type: {
continent_code: {type: String, required: true},
continent_name: {type: String, required: true},
country_code: {type: String, required: true},
country_name: {type: String, required: true},
region_code: {type: String, required: true},
region_name: {type: String, required: true},
city: {type: String, required: true},
zip: {type: String, required: true},
latitude: {type: Number, required: true},
longitude: {type: Number, required: true},
ipAddress: { type: String, required: true},
geolocation: { type: {
continent_code: { type: String, required: true },
continent_name: { type: String, required: true },
country_code: { type: String, required: true },
country_name: { type: String, required: true },
region_code: { type: String, required: true },
region_name: { type: String, required: true },
city: { type: String, required: true },
zip: { type: String, required: true },
latitude: { type: Number, required: true },
longitude: { type: Number, required: true },
}, required: false}
});

Expand Down

0 comments on commit 8f004ad

Please sign in to comment.