message: Fetch merchant info record from DB if content is not given
diff: @@ -19,27 +19,56 @@ interface JSONAPIDocument {
  export default class MerchantInfoSerializer {
  databaseManager: DatabaseManager = inject('database-manager', { as: 'databaseManager' });
  - async serialize(content: MerchantInfo): Promise<JSONAPIDocument> {
  - const did = encodeDID({ type: 'MerchantInfo', uniqueId: content.id });
  + async serialize(content: MerchantInfo | string): Promise<JSONAPIDocument> {
  + let data: MerchantInfo;
  + let did: string;
  - const data = {
  - id: content.id,
  + if (typeof content === 'string') {
  + did = encodeDID({ type: 'MerchantInfo', uniqueId: content });
  + data = await this.loadMerchantInfo(content);
  + } else {
  + did = encodeDID({ type: 'MerchantInfo', uniqueId: content.id });
  + data = content;
  + }
  +
  + const result = {
  + data: {
  + id: data.id,
  type: 'merchant-infos',
  attributes: {
  did,
  - name: content.name,
  - slug: content.slug,
  - color: content.color,
  - textColor: content.textColor,
  - ownerAddress: content.ownerAddress,
  + name: data.name,
  + slug: data.slug,
  + color: data.color,
  + textColor: data.textColor,
  + ownerAddress: data.ownerAddress,
  + },
  },
  };
  - const result = {
  - data,
  - } as JSONAPIDocument;
  + return result as JSONAPIDocument;
  + }
  +
  + async loadMerchantInfo(id: string): Promise<MerchantInfo> {
  + let db = await this.databaseManager.getClient();
  + let queryResult = await db.query(
  + 'SELECT id, name, slug, color, text_color, owner_address, created_at from merchant_infos WHERE id = $1',
  + [id]
  + );
  - return result;
  + if (queryResult.rowCount === 0) {
  + return Promise.reject(new Error(`No merchant_infos record found with id ${id}`));
  + }
  +
  + let row = queryResult.rows[0];
  + return {
  + id: row['id'],
  + name: row['name'],
  + slug: row['slug'],
  + color: row['color'],
  + textColor: row['text_color'],
  + ownerAddress: row['owner_address'],
  + };
  }
  }
  