calendarInfo.api.js 3.8 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
2
import { DEBUG, enablePromise } from 'react-native-sqlite-storage';
import getDb from './moneyDB'
Choi Ga Young's avatar
Choi Ga Young committed
3
import { getDateStr } from '../utils/dateFunction';
Choi Ga Young's avatar
Choi Ga Young committed
4
5
6
7

DEBUG(true);
enablePromise(true);

8
const getFullData = async ({ year, month }) => {
Choi Ga Young's avatar
Choi Ga Young committed
9
10
  const thisFirst = new Date(year, month, 1);
  const thisLast = new Date(year, month + 1, 0);
11

Choi Ga Young's avatar
Choi Ga Young committed
12
13
14
  const db = await getDb();
  return new Promise((res, rej) => {
    db.transaction(async (tx) => {
Choi Ga Young's avatar
Choi Ga Young committed
15
      const [txn, results] = await tx.executeSql(`SELECT date, type_id, sum(price) as total from money where date BETWEEN "${getDateStr(thisFirst)}" and "${getDateStr(thisLast)}" group by date, type_id`);
Choi Ga Young's avatar
Choi Ga Young committed
16
      const temp = [];
17
18
19
20
21
22
23
      if (results.rows.length != 0) {
        if (results.rows.item(0).type_id === 1) {
          temp.push({ date: results.rows.item(0).date, type: { 'input': results.rows.item(0).total } })
        }
        if (results.rows.item(0).type_id === 2) {
          temp.push({ date: results.rows.item(0).date, type: { 'output': results.rows.item(0).total } })
        }
Choi Ga Young's avatar
Choi Ga Young committed
24
      }
25

26
      for (let i = 1; i < results.rows.length; i++) {
Choi Ga Young's avatar
Choi Ga Young committed
27
28
29
        if (results.rows.item(i).type_id===3){
          continue
        }
30
31
32
33
34
        if (results.rows.item(i).date === results.rows.item(i - 1).date) {
          temp.pop()
          let { 1: input, 2: output } = {
            [results.rows.item(i).type_id]: results.rows.item(i).total,
            [results.rows.item(i - 1).type_id]: results.rows.item(i - 1).total,
Choi Ga Young's avatar
Choi Ga Young committed
35
          }
36
          temp.push({ date: results.rows.item(i).date, type: { 'input': input, 'output': output } })
Choi Ga Young's avatar
Choi Ga Young committed
37
        } else {
38
39
40
41
42
43
          if (results.rows.item(i).type_id === 1) {
            temp.push({ date: results.rows.item(i).date, type: { 'input': results.rows.item(i).total } })
          }
          if (results.rows.item(i).type_id === 2) {
            temp.push({ date: results.rows.item(i).date, type: { 'output': results.rows.item(i).total } })
          }
Choi Ga Young's avatar
Choi Ga Young committed
44
45
        }
      }
46
47
48
49
50
51
52
53
54
      res(temp)
    })
  })
}

const detailData = async ({ findDate }) => {
  const db = await getDb();
  return new Promise((res, rej) => {
    db.transaction(async (tx) => {
Choi Ga Young's avatar
Choi Ga Young committed
55
      const [txn, results] = await tx.executeSql(`SELECT money.money_id, assets_name, money.type_id, category_name, contents, price FROM money inner JOIN categories on money.category_id = categories.category_id inner JOIN assets_type on money.assets_id = assets_type.assets_id WHERE date="${findDate}"`);
56
57
      const temp = [];
      for (let i = 0; i < results.rows.length; i++) {
Choi Ga Young's avatar
Choi Ga Young committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        if (results.rows.item(i).type_id === 3) {
          let asset_type = results.rows.item(i).assets_name;
          let deposit_asset = results.rows.item(i).assets_name;
          let price = 0;
          if (results.rows.item(i).price > 0) {
            deposit_asset = results.rows.item(i).assets_name;
            asset_type = results.rows.item(i + 1).assets_name;
            price = results.rows.item(i).price;
          } else {
            asset_type = results.rows.item(i).assets_name;
            deposit_asset = results.rows.item(i + 1).assets_name;
            price = -results.rows.item(i).price;
          }
          temp.push({
            id: results.rows.item(i).money_id,
            category: results.rows.item(i).category_name,
            asset: asset_type,
            deposit_asset: deposit_asset,
            contents: results.rows.item(i).contents,
            price: price,
            type: results.rows.item(i).type_id
          })
          i++;
        } else {
          temp.push({ id: results.rows.item(i).money_id, 
            category: results.rows.item(i).category_name, 
            contents: results.rows.item(i).contents, 
            price: results.rows.item(i).price, 
            type: results.rows.item(i).type_id })
        }
88
89
      }
      res(temp);
Choi Ga Young's avatar
Choi Ga Young committed
90
91
92
93
94
    })
  })
}

const calApi = {
95
96
  getFullData,
  detailData
Choi Ga Young's avatar
Choi Ga Young committed
97
98
99
}

export default calApi;