Skip to content

Commit

Permalink
fixed utc conversion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Bone committed Nov 10, 2016
1 parent d6d1ed1 commit d135e58
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/decorators/attribute.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function Attribute(config: any = {}) {
let converter = function(dataType: any, value: any, forSerialisation = false): any {
if (!forSerialisation) {
if (dataType === Date) {
return moment.utc(value).toDate();
return moment(value).toDate();
}
} else {
if (dataType === Date) {
Expand Down
34 changes: 17 additions & 17 deletions src/models/json-api.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('JsonApiModel', () => {

describe('constructor', () => {

it('should be instanciated with attributes', () => {
it('should be instantiated with attributes', () => {
const DATA = {
id: '1',
attributes: {
Expand All @@ -48,10 +48,10 @@ describe('JsonApiModel', () => {
expect(author).toBeDefined();
expect(author.id).toBe('1');
expect(author.name).toBe('Daniele');
expect(author.date_of_birth.getTime()).toBe(moment('1987-05-25T00:00:00Z').toDate().getTime());
expect(author.date_of_birth.getTime()).toBe(moment('1987-05-25').toDate().getTime());
});

it('should be instanciated without attributes', () => {
it('should be instantiated without attributes', () => {
let author: Author = new Author(datastore);
expect(author).toBeDefined();
expect(author.id).toBeUndefined();
Expand All @@ -69,10 +69,10 @@ describe('JsonApiModel', () => {
expect(author).toBeDefined();
expect(author.id).toBe(AUTHOR_ID);
expect(author.name).toBe(AUTHOR_NAME);
expect(author.date_of_birth.valueOf()).toBe(moment.utc(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf());
expect(author.date_of_death.valueOf()).toBe(moment.utc(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf());
expect(author.created_at.valueOf()).toBe(moment.utc(AUTHOR_CREATED).valueOf());
expect(author.updated_at.valueOf()).toBe(moment.utc(AUTHOR_UPDATED).valueOf());
expect(author.date_of_birth.valueOf()).toBe(moment(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf());
expect(author.date_of_death.valueOf()).toBe(moment(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf());
expect(author.created_at.valueOf()).toBe(moment(AUTHOR_CREATED).valueOf());
expect(author.updated_at.valueOf()).toBe(moment(AUTHOR_UPDATED).valueOf());
expect(author.books).toBeUndefined();
});

Expand All @@ -86,17 +86,17 @@ describe('JsonApiModel', () => {
expect(author).toBeDefined();
expect(author.id).toBe(AUTHOR_ID);
expect(author.name).toBe(AUTHOR_NAME);
expect(author.date_of_birth.valueOf()).toBe(moment.utc(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf());
expect(author.date_of_death.valueOf()).toBe(moment.utc(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf());
expect(author.created_at.valueOf()).toBe(moment.utc(AUTHOR_CREATED).valueOf());
expect(author.updated_at.valueOf()).toBe(moment.utc(AUTHOR_UPDATED).valueOf());
expect(author.date_of_birth.valueOf()).toBe(moment(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf());
expect(author.date_of_death.valueOf()).toBe(moment(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf());
expect(author.created_at.valueOf()).toBe(moment(AUTHOR_CREATED).valueOf());
expect(author.updated_at.valueOf()).toBe(moment(AUTHOR_UPDATED).valueOf());
expect(author.books).toBeDefined();
expect(author.books.length).toBe(BOOK_NUMBER);
author.books.forEach((book: Book, index: number) => {
expect(book instanceof Book).toBeTruthy();
expect(+book.id).toBe(index + 1);
expect(book.title).toBe(BOOK_TITLE);
expect(book.date_published.valueOf()).toBe(moment.utc(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf());
expect(book.date_published.valueOf()).toBe(moment(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf());
});
});

Expand Down Expand Up @@ -125,17 +125,17 @@ describe('JsonApiModel', () => {
expect(author).toBeDefined();
expect(author.id).toBe(AUTHOR_ID);
expect(author.name).toBe(AUTHOR_NAME);
expect(author.date_of_birth.valueOf()).toBe(moment.utc(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf());
expect(author.date_of_death.valueOf()).toBe(moment.utc(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf());
expect(author.created_at.valueOf()).toBe(moment.utc(AUTHOR_CREATED).valueOf());
expect(author.updated_at.valueOf()).toBe(moment.utc(AUTHOR_UPDATED).valueOf());
expect(author.date_of_birth.valueOf()).toBe(moment(AUTHOR_BIRTH, 'YYYY-MM-DD').valueOf());
expect(author.date_of_death.valueOf()).toBe(moment(AUTHOR_DEATH, 'YYYY-MM-DD').valueOf());
expect(author.created_at.valueOf()).toBe(moment(AUTHOR_CREATED).valueOf());
expect(author.updated_at.valueOf()).toBe(moment(AUTHOR_UPDATED).valueOf());
expect(author.books).toBeDefined();
expect(author.books.length).toBe(BOOK_NUMBER);
author.books.forEach((book: Book, index: number) => {
expect(book instanceof Book).toBeTruthy();
expect(+book.id).toBe(index + 1);
expect(book.title).toBe(BOOK_TITLE);
expect(book.date_published.valueOf()).toBe(moment.utc(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf());
expect(book.date_published.valueOf()).toBe(moment(BOOK_PUBLISHED, 'YYYY-MM-DD').valueOf());
expect(book.chapters).toBeDefined();
expect(book.chapters.length).toBe(CHAPTERS_NUMBER);
book.chapters.forEach((chapter: Chapter, cindex: number) => {
Expand Down
20 changes: 14 additions & 6 deletions src/models/json-api.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ export class JsonApiModel {
if (relationship && relationship.data && relationship.data.length > 0) {
let typeName: string = relationship.data[0].type;
let modelType: ModelType<this> = Reflect.getMetadata('JsonApiDatastoreConfig', this._datastore.constructor).models[typeName];
let relationshipModel: JsonApiModel[] = this.getHasManyRelationship(modelType, relationship.data, included, typeName, level);
if (relationshipModel.length > 0) {
this[metadata.propertyName] = relationshipModel;
if (modelType) {
let relationshipModel: JsonApiModel[] = this.getHasManyRelationship(modelType, relationship.data, included, typeName, level);
if (relationshipModel.length > 0) {
this[metadata.propertyName] = relationshipModel;
}
} else {
throw {message: 'parseHasMany - Model type for relationship ' + typeName + ' not found.'};
}
}
}
Expand All @@ -89,9 +93,13 @@ export class JsonApiModel {
if (dataRelationship) {
let typeName: string = dataRelationship.type;
let modelType: ModelType<this> = Reflect.getMetadata('JsonApiDatastoreConfig', this._datastore.constructor).models[typeName];
let relationshipModel: JsonApiModel = this.getBelongsToRelationship(modelType, dataRelationship, included, typeName, level);
if (relationshipModel) {
this[metadata.propertyName] = relationshipModel;
if (modelType) {
let relationshipModel: JsonApiModel = this.getBelongsToRelationship(modelType, dataRelationship, included, typeName, level);
if (relationshipModel) {
this[metadata.propertyName] = relationshipModel;
}
} else {
throw { message: 'parseBelongsTo - Model type for relationship ' + typeName + ' not found.' };
}
}
}
Expand Down

0 comments on commit d135e58

Please sign in to comment.