Skip to content

Commit d3c3495

Browse files
author
Hans Kristian Flaatten
committed
feat(model): virtual properties to check app status
1 parent db3362c commit d3c3495

File tree

4 files changed

+85
-20
lines changed

4 files changed

+85
-20
lines changed

apps/app/controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ route.post('/new', (req, res, next) => {
100100
};
101101

102102
// Warn about pending app approval.
103-
if (!app.approved) {
103+
if (app.isPending) {
104104
req.session.message = {
105105
class: 'info',
106106
title: 'Venter på godkjenning',

apps/app/model.js

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ const appSchema = new Schema({
3333
},
3434
});
3535

36+
appSchema.virtual('isActive').get(function appSchemaIsActive() {
37+
return !!this.active && !!this.approved;
38+
});
39+
40+
appSchema.virtual('isPending').get(function appSchemaIsPending() {
41+
return !this.approved && !this.rejection;
42+
});
43+
44+
appSchema.virtual('isRejected').get(function appSchemaIsRejected() {
45+
return !this.approved && !!this.rejection;
46+
});
47+
3648
appSchema.virtual('slugg').get(function appSchemaVirtualSlugg() {
3749
return this.name.toLowerCase().replace(/[^a-zaæå0-9]/g, '-');
3850
});

test/models/appSchema.js

+39
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,45 @@ beforeEach(done => {
1313
});
1414

1515
describe('appSchema', () => {
16+
describe('isActive()', () => {
17+
[true, false].forEach(active => {
18+
[true, false].forEach(approved => {
19+
const ret = active && approved;
20+
21+
it(`returns ${ret} when active=${active} and approved=${approved}`, () => {
22+
const app = api.apps.create({ active, approved });
23+
assert.equal(app.isActive, ret);
24+
});
25+
});
26+
});
27+
});
28+
29+
describe('isPending()', () => {
30+
[true, false].forEach(approved => {
31+
[undefined, 'Rejected'].forEach(rejection => {
32+
const ret = !approved && !rejection;
33+
34+
it(`returns ${ret} when approved=${approved} and rejection=${rejection}`, () => {
35+
const app = api.apps.create({ approved, rejection });
36+
assert.equal(app.isPending, ret);
37+
});
38+
});
39+
});
40+
});
41+
42+
describe('isRejected()', () => {
43+
[true, false].forEach(approved => {
44+
[undefined, 'Rejected'].forEach(rejection => {
45+
const ret = !approved && !!rejection;
46+
47+
it(`returns ${ret} when approved=${approved} and rejection=${rejection}`, () => {
48+
const app = api.apps.create({ approved, rejection });
49+
assert.equal(app.isRejected, ret);
50+
});
51+
});
52+
});
53+
});
54+
1655
describe('slugg', () => {
1756
it('returns URL safe slug for simple name', () => {
1857
const app = api.apps.create({ name: 'My App' });

views/app/index.html

+33-19
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h4 class="ui header">
3434
<div class="ui attached message">
3535
<div class="header">{{ app.name }}</div>
3636
</div>
37-
{% if app.active or (not app.approved and not app.rejection) %}
37+
{% if app.isActive or app.isPending %}
3838
<form class="ui form attached fluid segment" method="post" action="/app/{{ app._id }}">
3939
<div class="two fields">
4040
<div class="field">
@@ -54,68 +54,82 @@ <h4 class="ui header">
5454
<div class="twelve wide field">
5555
<label>Prod Nøkkel</label>
5656
<div class="ui action input">
57-
<input type="text" name="key_prod" value="{{ app.key.prod }}"
57+
<input
58+
type="text"
59+
name="key_prod"
60+
value="{{ app.key.prod }}"
5861
readonly="">
59-
<button class="ui teal right labeled icon button copy"
62+
<button
63+
class="ui teal right labeled icon button copy"
6064
data-clipboard-text="{{ app.key.prod }}">
61-
<i class="copy icon"></i> Kopier
65+
<i class="copy icon"></i> Kopier
6266
</button>
6367
</div>
6468
</div>
6569
<div class="four wide field">
6670
<label>Rate Limit</label>
67-
<input type="text" name="limit_prod"
71+
<input
72+
type="text"
73+
name="limit_prod"
74+
placeholder="{{ app.limit.prod }}"
6875
value="{{ app.limit.prodRequest | d(app.limit.prod) }}">
6976
</div>
7077
</div>
7178
<div class="fields">
7279
<div class="twelve wide field">
7380
<label>Dev Nøkkel</label>
7481
<div class="ui action input">
75-
<input type="text" name="key_dev" value="{{ app.key.dev }}"
82+
<input
83+
type="text"
84+
name="key_dev"
85+
value="{{ app.key.dev }}"
7686
readonly="">
77-
<button class="ui teal right labeled icon button copy"
87+
<button
88+
class="ui teal right labeled icon button copy"
7889
data-clipboard-text="{{ app.key.dev }}">
79-
<i class="copy icon"></i> Kopier
90+
<i class="copy icon"></i> Kopier
8091
</button>
8192
</div>
8293
</div>
8394
<div class="four wide field">
8495
<label>Rate Limit</label>
85-
<input type="text" name="limit_dev"
96+
<input
97+
type="text"
98+
name="limit_dev"
99+
placeholder="{{ app.limit.dev }}"
86100
value="{{ app.limit.devRequest | d(app.limit.dev) }}">
87101
</div>
88102
</div>
89-
<button class="ui blue submit button" name="save" value="true">
103+
<button class="ui blue submit button" type="submit">
90104
Lagre
91105
</button>
92-
{% if app.active %}
93-
<button class="ui red submit button" name="deactivate" value="true">
106+
{% if app.isActive %}
107+
<button class="ui red submit button" type="submit">
94108
Deaktiver
95109
</button>
96110
{% endif %}
97111
</form>
98112
{% endif %}
99-
{% if app.rejection %}
113+
{% if app.isRejected %}
100114
<div class="ui bottom attached error message">
101115
<i class="icon warning"></i>
102116
Denne applikasjonen ble avvist. {{ app.rejection }}
103117
</div>
104-
{% elif not app.approved %}
118+
{% elif app.isPending %}
105119
<div class="ui bottom attached info message">
106120
<i class="icon wait"></i>
107121
Denne applikasjonen venter på godkjenning.
108122
</div>
109-
{% elif not app.active %}
110-
<div class="ui bottom attached warning message">
111-
<i class="icon warning"></i>
112-
Denne applikasjonen er deaktivert.
113-
</div>
114123
{% elif app.limit.devRequest or app.limit.prodRequest %}
115124
<div class="ui bottom attached info message">
116125
<i class="icon wait"></i>
117126
Endring av API rate-limit venter på godkjenning.
118127
</div>
128+
{% elif not app.isActive %}
129+
<div class="ui bottom attached warning message">
130+
<i class="icon warning"></i>
131+
Denne applikasjonen er deaktivert.
132+
</div>
119133
{% endif %}
120134
</div>
121135
{% endfor %}

0 commit comments

Comments
 (0)