Skip to content

Commit

Permalink
refactor(service): users/auth: remove references to user icon type pr…
Browse files Browse the repository at this point in the history
…operty which did not serve any real purpose
  • Loading branch information
restjohn committed Sep 13, 2024
1 parent 88eee2a commit 21b9d7f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 51 deletions.
6 changes: 2 additions & 4 deletions service/src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const UserModel = require('../models/user')
, LoginModel = require('../models/login')
, DeviceModel = require('../models/device')
, TeamModel = require('../models/team')
// TODO: users-next
, AuthenticationConfiguration = require('../models/authenticationconfiguration')
, path = require('path')
, fs = require('fs-extra')
Expand Down Expand Up @@ -145,18 +146,15 @@ User.prototype.create = async function (user, options = {}) {
} catch { }
}

if (options.icon && (options.icon.type === 'create' || options.icon.type === 'upload')) {
if (options.icon) {
try {
const icon = iconPath(newUser._id, newUser, options.icon);
await fs.move(options.icon.path, icon.absolutePath);

newUser.icon.type = options.icon.type;
newUser.icon.relativePath = icon.relativePath;
newUser.icon.contentType = options.icon.mimetype;
newUser.icon.size = options.icon.size;
newUser.icon.text = options.icon.text;
newUser.icon.color = options.icon.color;

await newUser.save();
} catch { }
}
Expand Down
17 changes: 3 additions & 14 deletions service/src/entities/users/entities.users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,17 @@ export interface Phone {
}

/**
* TODO: There is not much value to retaining the `type`, `text`, and `color` attributes. Only the web app's user
* admin screen uses these to set default form values, but the web app always generates a raster png from those values
* anyway.
* A user's icon is the image that appears on the map to indicate the user's location. If the icon has text and color,
* a client can choose to render that rather than the raster image the server stores.
*/
export interface UserIcon {
/**
* Type defaults to {@link UserIconType.None} via database layer.
*/
type: UserIconType
text?: string
color?: string
contentType?: string
size?: number
relativePath?: string
}

export enum UserIconType {
None = 'none',
Upload = 'upload',
Create = 'create',
}

export interface Avatar {
contentType?: string,
size?: number,
Expand All @@ -83,7 +72,7 @@ export interface UserRepository {
findAllByIds(ids: UserId[]): Promise<{ [id: string]: User | null }>
find<MappedResult>(which?: UserFindParameters, mapping?: (user: User) => MappedResult): Promise<PageOf<MappedResult>>
saveMapIcon(userId: UserId, icon: UserIcon, content: NodeJS.ReadableStream | Buffer): Promise<User | UserRepositoryError>
saveAvatar(avatar: Avatar, content: NodeJS.ReadableStream | Buffer): Promise<User | UserRepositoryError>
saveAvatar(userId: UserId, avatar: Avatar, content: NodeJS.ReadableStream | Buffer): Promise<User | UserRepositoryError>
deleteMapIcon(userId: UserId): Promise<User | UserRepositoryError>
deleteAvatar(userId: UserId): Promise<User | UserRepositoryError>
}
Expand Down
32 changes: 18 additions & 14 deletions service/src/migrations/007-user-icon.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
const async = require('async')
, User = require('../models/user');
// const async = require('async')
// , User = require('../models/user');

exports.id = '007-user-icon';

/**
* This migration became obsolete after removing the `type` property from the user icon document.
*/
exports.up = function(done) {
this.log('updating user icons');
done();

// TODO: users-next
User.getUsers(function(err, users) {
if (err) return done(err);
// this.log('updating user icons');
// // TODO: users-next
// User.getUsers(function(err, users) {
// if (err) return done(err);

async.each(users, function(user, done) {
user.icon = user.icon || {};
user.icon.type = user.icon.relativePath ? 'upload' : 'none';
user.save(done);
}, function(err) {
done(err);
});
});
// async.each(users, function(user, done) {
// user.icon = user.icon || {};
// user.icon.type = user.icon.relativePath ? 'upload' : 'none';
// user.save(done);
// }, function(err) {
// done(err);
// });
// });
};

exports.down = function(done) {
Expand Down
19 changes: 2 additions & 17 deletions service/src/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,15 @@ module.exports = function (app, security) {

function parseIconUpload(req, res, next) {
let iconMetadata = req.param('iconMetadata') || {};
if (typeof iconMetadata === 'string' || iconMetadata instanceof String) {
if (typeof iconMetadata === 'string') {
iconMetadata = JSON.parse(iconMetadata);
}

const files = req.files || {};
let [icon] = files.icon || [];
let [ icon ] = files.icon || [];
if (icon) {
// default type to upload
if (!iconMetadata.type) iconMetadata.type = 'upload';

if (iconMetadata.type !== 'create' && iconMetadata.type !== 'upload') {
return res.status(400).send('invalid icon metadata');
}

icon.type = iconMetadata.type;
icon.text = iconMetadata.text;
icon.color = iconMetadata.color;
} else if (iconMetadata.type === 'none') {
icon = {
type: 'none'
};
files.icon = [icon];
}

next();
}

Expand Down
3 changes: 1 addition & 2 deletions web-app/src/ng1/admin/users/user.edit.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ class AdminUserEditController {
if (this.$stateParams.userId) {
this.UserService.getUser(this.$stateParams.userId).then(user => {
this.user = angular.copy(user);

this.iconMetadata = {
type: this.user.icon.type,
type: this.user.icon.type || 'upload',
text: this.user.icon.text,
color: this.user.icon.color
};
Expand Down

0 comments on commit 21b9d7f

Please sign in to comment.