Skip to content

Commit

Permalink
🎨 DenoKVを渡して必要なデータだけ返すようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
dicenull committed Sep 3, 2024
1 parent 838cd38 commit 340dda3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
21 changes: 16 additions & 5 deletions library/distilled_user_within_24hour.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ async function distilledUserWithin24Hours(id, kv) {
const nowTime = await getNowTime(); //今現在の時間

for await (const element of spIterator) {
if (new Date(nowTime) - new Date(element.value.time) < 86400 * 1000) { //24時間以内の場合
const sourceId = element.key[1];
const destinationId = element.value.User;
const time = element.value.time;

if (new Date(nowTime) - new Date(time) < 86400 * 1000) { //24時間以内の場合
if (
element.key[1] === Number(id) ||
Number(element.value.User) === Number(id)
) { //自身に送られたまたは自身が送ったなら
distilledUserArray.push(element);
sourceId === Number(id) ||
Number(destinationId) === Number(id)
) {
//自身に送られたまたは自身が送ったなら
const result = {
sourceId,
destinationId,
time,
};

distilledUserArray.push(result);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions public/daily_result/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ window.onload = async (event) => {
const ID = Number(JSON.parse(myData).id); //自身のID取得

// 各イテレーションで配列の要素セット
let currentIterateUsername = data[step].key[1];
if (ID === data[step].key[1]) {
currentIterateUsername = data[step].value.User;
let currentIterateUsername = data[step].sourceId;
if (ID === data[step].sourceId) {
currentIterateUsername = data[step].destinationId;
} //自身の送信したものなら送信先を見る
const currentIterateContactTime = data[step].value.time;
const currentIterateContactTime = data[step].time;

// 新しいdiv要素を作成
const outDiv = document.createElement("div");
Expand Down
33 changes: 12 additions & 21 deletions tests/distilled_user_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@ import { distilledUserWithin24Hours } from "../library/distilled_user_within_24h
import { getNowTime } from "../library/get_time.js";

Deno.test("24時間以内に交流したユーザだけ取得できる", async () => {
const user1 = {
key: ["SP", 1, 2],
value: {
User: 2,
time: getNowTime(),
},
};
const user2 = {
key: ["SP", 2, 3],
value: {
User: 3,
time: getNowTime(),
},
};
const kv = {
list: async function* () {
yield user1;
yield user2;
},
};
const kv = await Deno.openKv();
const user1 = { User: 2, time: getNowTime() };
const user2 = { User: 3, time: getNowTime() };
kv.set(["SP", 1, 2], user1);
kv.set(["SP", 2, 3], user2);

const response = await distilledUserWithin24Hours(1, kv);

const data = await response.json();
assertEquals(data, [user1]);
assertEquals(data, [{
sourceId: 1,
destinationId: 2,
time: user1.time,
}]);

await kv.close();
});

0 comments on commit 340dda3

Please sign in to comment.