Skip to content

Commit 3fbc855

Browse files
committed
Merge branch 'main_server' of github.com:AlmostGreatBand/lemon-server into heroku-multiapp
2 parents 422c163 + 11680d3 commit 3fbc855

3 files changed

Lines changed: 11 additions & 27 deletions

File tree

src/datasources/LemonRepository.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class LemonRepository {
5858
return { ok: false, error: new Error('Wrong password') };
5959
}
6060
const ok = this.db.setBank(profile.account_id, bank);
61-
return { ok, error: null };
61+
if (!ok) return { ok, error: new Error('Database error') };
62+
return setCards(bank);
6263
}
6364

6465
/** @return Array<Card>, error */
@@ -74,22 +75,8 @@ class LemonRepository {
7475
}
7576

7677
/** @return boolean, error */
77-
async setCard(user) {
78+
async setCards(bank) {
7879
try {
79-
const profile = this.db.getAccount(user.login);
80-
if (!profile) {
81-
return { ok: false, error: new Error('Account Not Found') };
82-
}
83-
if (!profile.token) {
84-
return { ok: false, error: new Error('Account Has No Token') };
85-
}
86-
if (user.password !== profile.password) {
87-
return { ok: false, error: new Error('Wrong password') };
88-
}
89-
const bank = this.db.getMonobank(profile);
90-
if (!bank) {
91-
return { ok: false, error: new Error('No cards present') };
92-
}
9380
const monoCards = await this.monoDS.getAccounts(bank.token);
9481
const monoAccounts = monoCards.accounts;
9582
const ok = this.db.setMonoCards(monoAccounts, profile);
@@ -114,17 +101,14 @@ class LemonRepository {
114101
try {
115102
const profile = this.db.getAccount(user.login);
116103
if (!profile) {
117-
throw new Error('Account Not Found');
118-
}
119-
if (!profile.token) {
120-
return { transactions: null, error: new Error('Account Has No Token') };
104+
return { transactions: null, error: new Error('Account Not Found') };
121105
}
122106
if (user.password !== profile.password) {
123-
throw new Error('Wrong password');
107+
return { transactions: null, error: new Error('Wrong password') };
124108
}
125109
const bank = this.db.getMonobank(profile);
126110
if (!bank) {
127-
throw new Error('No cards present');
111+
return { transactions: null, error: new Error('No cards present') };
128112
}
129113
const cards = this.db.getCards(profile.account_id);
130114
const accounts = this._formMonoTrRequest(cards);

src/routing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const routing = {
8888
'POST': {
8989
'/': () => makeResponse(200, 'Welcome to Lemon&#127819 Server!'),
9090
'/profile/': (user, newInfo) => changeUserInfo(user, newInfo),
91-
'/cards/': user => setCards(user),
91+
'/banks/add/': async (user, bank) => setBank(user, bank),
9292
'/transactions/': (user, transaction) => addTransaction(user, transaction),
9393
'/registration/': (_, user) => registerUser(user),
9494
},

src/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const authorizeUser = (req, res) => {
2828
};
2929

3030
const createServer = (protocol, options) => (
31-
protocol.createServer(options, (req, res) => {
31+
protocol.createServer(options, async (req, res) => {
3232
const credentials = authorizeUser(req, res);
3333
if (!credentials) return;
3434
const handler = routing[req.method][normalizePath(req.url)];
@@ -45,13 +45,13 @@ const createServer = (protocol, options) => (
4545
req.on('data', chunk => {
4646
body += chunk.toString();
4747
});
48-
req.on('end', () => {
49-
const { code, data } = handler(user, body);
48+
req.on('end', async () => {
49+
const { code, data } = await handler(user, body);
5050
res.writeHead(code);
5151
res.end(JSON.stringify(data));
5252
})
5353
} else {
54-
const { code, data } = handler(user);
54+
const { code, data } = await handler(user);
5555
res.writeHead(code);
5656
res.end(JSON.stringify(data));
5757
}

0 commit comments

Comments
 (0)