Skip to content

Commit 1b6da62

Browse files
committed
chore: sync upstream
1 parent af5ce0d commit 1b6da62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3317
-1684
lines changed

template/api.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
const API = require('@ladjs/api');
22
const Graceful = require('@ladjs/graceful');
33
const Mongoose = require('@ladjs/mongoose');
4-
const _ = require('lodash');
54
const ip = require('ip');
65

7-
const config = require('./config');
8-
const routes = require('./routes');
9-
const i18n = require('./helpers/i18n');
106
const logger = require('./helpers/logger');
11-
const passport = require('./helpers/passport');
7+
const apiConfig = require('./config/api');
128

13-
const api = new API({
14-
routes: routes.api,
15-
logger,
16-
i18n,
17-
passport
18-
});
9+
const api = new API(apiConfig);
1910

2011
if (!module.parent) {
21-
const mongoose = new Mongoose(
22-
_.merge(
23-
{
24-
logger
25-
},
26-
api.config.mongoose,
27-
config.mongoose
28-
)
29-
);
12+
const mongoose = new Mongoose({ ...api.config.mongoose, logger });
3013

3114
const graceful = new Graceful({
3215
mongooses: [mongoose],
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,59 @@
11
const Boom = require('@hapi/boom');
2+
const _ = require('lodash');
23
const isSANB = require('is-string-and-not-blank');
3-
const { select } = require('mongoose-json-select');
44

5+
const sendVerificationEmail = require('../../../../helpers/send-verification-email');
56
const config = require('../../../../config');
67
const { Users } = require('../../../models');
78

89
async function create(ctx) {
910
const { body } = ctx.request;
1011

1112
if (!isSANB(body.password))
12-
return ctx.throw(Boom.badRequest(ctx.translate('INVALID_PASSWORD')));
13+
return ctx.throw(Boom.badRequest(ctx.translateError('INVALID_PASSWORD')));
1314

1415
// register the user
15-
const query = { email: body.email };
16+
const query = { email: body.email, locale: ctx.locale };
1617
query[config.userFields.hasVerifiedEmail] = false;
1718
query[config.userFields.hasSetPassword] = true;
1819
query[config.userFields.pendingRecovery] = false;
19-
const user = await Users.register(query, body.password);
20+
query[config.lastLocaleField] = ctx.locale;
21+
22+
ctx.state.user = await Users.register(query, body.password);
2023

2124
// send a verification email
22-
await user.sendVerificationEmail();
25+
ctx.state.user = await sendVerificationEmail(ctx);
2326

2427
// send the response
25-
const object = select(user.toObject(), Users.schema.options.toJSON.select);
26-
object[config.userFields.apiToken] = user[config.userFields.apiToken];
28+
const object = ctx.state.user.toObject();
29+
object[config.userFields.apiToken] =
30+
ctx.state.user[config.userFields.apiToken];
2731
ctx.body = object;
2832
}
2933

3034
async function retrieve(ctx) {
3135
// since we already have the user object
3236
// just send it over as a response
33-
ctx.body = ctx.state.user;
37+
ctx.body = ctx.state.user.toObject();
3438
}
3539

3640
async function update(ctx) {
3741
const { body } = ctx.request;
3842

39-
ctx.state.user.email = body.email;
40-
ctx.state.user[config.passport.fields.givenName] =
41-
body[config.passport.fields.givenName];
42-
ctx.state.user[config.passport.fields.familyName] =
43-
body[config.passport.fields.familyName];
44-
ctx.state.user.avatar_url = body.avatar_url;
43+
if (_.isString(body.email)) ctx.state.user.email = body.email;
44+
45+
if (_.isString(body[config.passport.fields.givenName]))
46+
ctx.state.user[config.passport.fields.givenName] =
47+
body[config.passport.fields.givenName];
48+
49+
if (_.isString(body[config.passport.fields.familyName]))
50+
ctx.state.user[config.passport.fields.familyName] =
51+
body[config.passport.fields.familyName];
52+
53+
if (_.isString(body.avatar_url)) ctx.state.user.avatar_url = body.avatar_url;
4554

46-
ctx.body = await ctx.state.user.save();
55+
ctx.state.user = await ctx.state.user.save();
56+
ctx.body = ctx.state.user.toObject();
4757
}
4858

4959
module.exports = { create, retrieve, update };

template/app/controllers/web/admin/users.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ async function list(ctx) {
2727

2828
async function retrieve(ctx) {
2929
ctx.state.result = await Users.findById(ctx.params.id);
30-
if (!ctx.state.result) throw new Error(ctx.translate('INVALID_USER'));
30+
if (!ctx.state.result) throw ctx.translateError('INVALID_USER');
3131
await ctx.render('admin/users/retrieve');
3232
}
3333

3434
async function update(ctx) {
3535
const user = await Users.findById(ctx.params.id);
36-
if (!user) throw new Error(ctx.translate('INVALID_USER'));
36+
if (!user) throw ctx.translateError('INVALID_USER');
3737
const { body } = ctx.request;
3838

3939
user[config.passport.fields.givenName] =
@@ -68,7 +68,7 @@ async function update(ctx) {
6868

6969
async function remove(ctx) {
7070
const user = await Users.findById(ctx.params.id);
71-
if (!user) throw new Error(ctx.translate('INVALID_USER'));
71+
if (!user) throw ctx.translateError('INVALID_USER');
7272
await user.remove();
7373
ctx.flash('custom', {
7474
title: ctx.request.t('Success'),
@@ -86,7 +86,7 @@ async function remove(ctx) {
8686

8787
async function login(ctx) {
8888
const user = await Users.findById(ctx.params.id);
89-
if (!user) throw new Error(ctx.translate('INVALID_USER'));
89+
if (!user) throw ctx.translateError('INVALID_USER');
9090

9191
ctx.logout();
9292

0 commit comments

Comments
 (0)