Skip to content

Commit

Permalink
feat: user locale data (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 21, 2019
1 parent 571032b commit d131670
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The [getting started guide](./docs/getting-started.md) has more information on u
## Status

[![Pipeline status](https://img.shields.io/gitlab/pipeline/ssube/isolex.svg?gitlab_url=https%3A%2F%2Fgit.apextoaster.com&logo=gitlab)](https://git.apextoaster.com/ssube/isolex/commits/master)
[![Test coverage](https://codecov.io/gh/ssube/isolex/branch/master/graph/badge.svg)](https://codecov.io/gh/ssube/isolex)
[![Test coverage](https://sonarcloud.io/api/project_badges/measure?project=ssube_isolex&metric=coverage)](https://sonarcloud.io/dashboard?id=ssube_isolex)
[![MIT license](https://img.shields.io/github/license/ssube/isolex.svg)](https://github.com/ssube/isolex/blob/master/LICENSE.md)

[![Open bug count](https://img.shields.io/github/issues-raw/ssube/isolex/type-bug.svg)](https://github.com/ssube/isolex/issues?q=is%3Aopen+is%3Aissue+label%3Atype%2Fbug)
Expand All @@ -25,6 +25,7 @@ The [getting started guide](./docs/getting-started.md) has more information on u
[![Dependency status](https://img.shields.io/david/ssube/isolex.svg)](https://david-dm.org/ssube/isolex)
[![Dev dependency status](https://img.shields.io/david/dev/ssube/isolex.svg)](https://david-dm.org/ssube/isolex?type=dev)

[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=ssube_isolex&metric=ncloc)](https://sonarcloud.io/dashboard?id=ssube_isolex)
[![Maintainability score](https://api.codeclimate.com/v1/badges/5d4326d6f68a2fa137cd/maintainability)](https://codeclimate.com/github/ssube/isolex/maintainability)
[![Technical debt ratio](https://img.shields.io/codeclimate/tech-debt/ssube/isolex.svg)](https://codeclimate.com/github/ssube/isolex/trends/technical_debt)
[![Quality issues](https://img.shields.io/codeclimate/issues/ssube/isolex.svg)](https://codeclimate.com/github/ssube/isolex/issues)
Expand Down
3 changes: 2 additions & 1 deletion src/controller/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BaseController, BaseControllerOptions, ErrorReplyType } from 'src/contr
import { createCommandCompletion } from 'src/controller/helpers';
import { Role } from 'src/entity/auth/Role';
import { Token } from 'src/entity/auth/Token';
import { User } from 'src/entity/auth/User';
import { LOCALE_DEFAULT, User } from 'src/entity/auth/User';
import { UserRepository } from 'src/entity/auth/UserRepository';
import { Command, CommandVerb } from 'src/entity/Command';
import { Context } from 'src/entity/Context';
Expand Down Expand Up @@ -104,6 +104,7 @@ export class AccountController extends BaseController<AccountControllerData> imp
},
});
const user = await this.userRepository.save(new User({
locale: LOCALE_DEFAULT,
name,
roles,
}));
Expand Down
3 changes: 2 additions & 1 deletion src/controller/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { INJECT_STORAGE } from 'src/BotService';
import { CheckRBAC, Controller, ControllerData, Handler } from 'src/controller';
import { BaseController, BaseControllerOptions } from 'src/controller/BaseController';
import { Role } from 'src/entity/auth/Role';
import { User } from 'src/entity/auth/User';
import { LOCALE_DEFAULT, User } from 'src/entity/auth/User';
import { UserRepository } from 'src/entity/auth/UserRepository';
import { Command, CommandVerb } from 'src/entity/Command';
import { Context } from 'src/entity/Context';
Expand Down Expand Up @@ -88,6 +88,7 @@ export class UserController extends BaseController<UserControllerData> implement
this.logger.debug({ roles }, 'found roles');

const user = await this.userRepository.save(new User({
locale: LOCALE_DEFAULT,
name,
roles,
}));
Expand Down
31 changes: 31 additions & 0 deletions src/entity/auth/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,41 @@ import { GRAPH_OUTPUT_ROLE, Role } from 'src/entity/auth/Role';
import { BaseEntity, BaseEntityOptions } from 'src/entity/base/BaseEntity';
import { doesExist } from 'src/utils';

export interface UserLocale {
date: string;
lang: string;
time: string;
timezone: string;
}

export interface UserOptions extends BaseEntityOptions {
locale: UserLocale;
name: string;
roles: Array<Role>;
}

/**
* TODO: do this without hard-coded fallbacks
*/
export const LOCALE_DEFAULT = {
date: 'YYYY-MM-DD',
lang: 'en-US',
time: 'HH:MM:SSZ',
timezone: 'GMT',
};

export const TABLE_USER = 'user';

@Entity(TABLE_USER)
export class User extends BaseEntity implements UserOptions {
@PrimaryGeneratedColumn('uuid')
public id?: string;

@Column({
type: 'simple-json',
})
public locale: UserLocale;

@Column({
unique: true,
})
Expand All @@ -34,8 +57,16 @@ export class User extends BaseEntity implements UserOptions {
super(options);

if (doesExist(options)) {
this.locale = {
...LOCALE_DEFAULT,
...options.locale,
};
this.name = options.name;
this.roles = options.roles;
} else {
this.locale = {
...LOCALE_DEFAULT,
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ParserModule } from 'src/module/ParserModule';
import { ServiceModule } from 'src/module/ServiceModule';
import { TransformModule } from 'src/module/TransformModule';
import { Schema } from 'src/schema';
import { ServiceEvent, ServiceDefinition } from 'src/Service';
import { ServiceDefinition, ServiceEvent } from 'src/Service';
import { BunyanLogger } from 'src/utils/BunyanLogger';
import { signal, SIGNAL_RELOAD, SIGNAL_RESET, SIGNAL_STOP } from 'src/utils/Signal';
import { VERSION_INFO } from 'src/version';
Expand Down
19 changes: 19 additions & 0 deletions src/migration/0001548049058-UserLocale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

import { TABLE_USER } from 'src/entity/auth/User';

const COLUMN_LOCALE = new TableColumn({
default: '\'{}\'',
name: 'locale',
type: 'varchar',
});

export class UserLocale0001548049058 implements MigrationInterface {
public async up(query: QueryRunner): Promise<void> {
await query.addColumn(TABLE_USER, COLUMN_LOCALE);
}

public async down(query: QueryRunner): Promise<void> {
await query.dropColumn(TABLE_USER, COLUMN_LOCALE);
}
}
2 changes: 2 additions & 0 deletions src/module/MigrationModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { KeywordCommand0001545509108 } from 'src/migration/0001545509108-Keyword
import { CreateTick0001546063195 } from 'src/migration/0001546063195-CreateTick';
import { Dates0001546236755 } from 'src/migration/0001546236755-Dates';
import { FragmentUser0001546283532 } from 'src/migration/0001546283532-FragmentUser';
import { UserLocale0001548049058 } from 'src/migration/0001548049058-UserLocale';

export class MigrationModule extends Module {
public async configure(options: ModuleOptions): Promise<void> {
Expand All @@ -32,6 +33,7 @@ export class MigrationModule extends Module {
CreateTick0001546063195,
Dates0001546236755,
FragmentUser0001546283532,
UserLocale0001548049058,
]);
}
}
4 changes: 3 additions & 1 deletion test/entity/TestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import { ineeda } from 'ineeda';

import { Role } from 'src/entity/auth/Role';
import { User } from 'src/entity/auth/User';
import { LOCALE_DEFAULT, User } from 'src/entity/auth/User';
import { Context } from 'src/entity/Context';
import { Listener } from 'src/listener';

Expand All @@ -20,6 +20,7 @@ describeAsync('context entity', async () => {
source: ineeda<Listener>(),
uid: 'test',
user: new User({
locale: LOCALE_DEFAULT,
name: 'test',
roles: [new Role({
grants,
Expand Down Expand Up @@ -47,6 +48,7 @@ describeAsync('context entity', async () => {
source: ineeda<Listener>(),
uid: 'test',
user: new User({
locale: LOCALE_DEFAULT,
name: 'test',
roles: [new Role({
grants,
Expand Down

0 comments on commit d131670

Please sign in to comment.